Menu Close
Python List tutorial

In this tutorial, you will learn about Python List and Python list methods. In Python list is an object that contains a collection of items.

The list provides various list methods that make it easy to work with the Python list. In previous we have seen all about the Python Pass statement.

In this guide, we will see What is a list in Python and some important Python list functions.

What is Python list ?

The list is a collection data type in python that stores collections of elements that are ordered, changeable, or mutable and indexed.

List in Python allows duplicate members that means you can assign duplicate elements inside the list in more than one place.

Python list is written in a square bracket ( [ ] ). To understand the list in Python we will create a list example.

myList = [1,2,4,5,6,7,7,8,9,9]
print(myList)

Ordered

The list ordered means, List items have defined order and that order can not be changed. When you will add a new item to the list, the new item will be added at the end of the list.

Changeable

List are changeable or mutable, Once you have created the list, you can change it for example add item, remove item, etc.

Duplicate Item

List in Python allow duplicate items, meaning you can place same item more than one place in the list.

Data Type of list.

If you want to know the data type of list, Then you will need to use type() built-in function.

myList = [1,2,4,5,6,7,7,8,9,9]
print(type(myList))

Access item from the Python list.

To access the list item, you can use index number, that means with the help of index number you can access any element of the list.

The index starts at 0. The index number is written in the square bracket ( [ ] ).

myList = [1,2,4,5,6,7,7,8,9,9]
print(myList[0])

Python supports negative indexing that means you can use a negative index number to access the list elements from the last of the list. The negative index starts from -1.

In myList list, -1 index element is 9 and -2 index element is 9 and so on.

myList = [1,2,4,5,6,7,7,8,9,9]
print(myList[-1])

Slice of List in Python.

Sometimes you need to require some data from the list that lie between specific range, Then you can use slicing.

In Python slicing, you have to define start and end index number for result.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(fruits[2:5])

Output will be: [‘kiwi’,’mango’,’melon’]

As you know that in list sclicing, the end index number element not included in the result, for example in the above result apple index number is 5 who not include in the result.

Negative Slicing:

You can also use negative slicing in the python list the same as slicing. In negative slicing, you will need to define the negative index number instead of the positive index.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(fruits[-5:-1])

Output will be: [‘kiwi’,’mango’,’melon’,’apple’]

Change value in List in Python:

Sometime you will need to change specific item in list, you can use index number to change value.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits[0] = 'grapes'
print(fruits)

Loop through a Python list:

You can display the list items using loop. To display item from list, use for loop.

Example

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
for fruit in fruits:
	print(fruit)

Output

orange
banana
kiwi
mango
melon
apple
pineapple

Length of the list:

To find the length of list in Python you can use len() built-in function.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(len(fruits))

Add item in list:

To add new item in list you need to use append() Python list method.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.append('Dates')
print(fruits)

If you want to add an item in the list on a specific index number then you can use the insert() Python method. insert method takes two parameters first is the index number and the second is the item.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.insert(2,'Blueberries')
print(fruits)

Remove item from list:

a. To remove specific items from the list you need to use the remove() method.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.remove('pineapple)
print(fruits

b. To remove specific index item from the list, use pop() method.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.pop(6,'pineapple)
print(fruits)

Note:- if index number is not specified then pop() method remove last index item.

del keyword use in Python list:

a. To delete specific items from list, use del keyword.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
del fruits[0]
print(fruits)

b. If you want delete complete list, use del keyword.

fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
del fruits
print(fruits)

Python List Methods:

Python has a set of built-in methods that you can use on lists.

Python List MethodsDescription
append()Python append() list method is used to add elements to the end of the list.
clear()Python clear() method is used to clear the list.
copy()Python copy() method is used to copy the list.
count()Python count() return the total number of times, an element appears in the list.
extend()Python extend() method is used to add the elements of list to the end current list.
insert()Python insert() method is used to add the element to the specific position.
index()Returns the index of the first element with the specified value.
pop()Python pop() method is used to remove the element from a specific position.
reverse()Reverse the order of the list.
sort()Python sort() list method is used to sort the list.

Conclusion

In this Online Python list tutorial, you have learned what is Python list and how to create a list, and some important list functions.

If you want to learn details about any list methods, Click on any listed method defined in the above table and learn them.

I hope you will like this post. if this post is helpful please visit and share it with your friends who want to learn Python programming from scratch to advanced.

Previous Article
Next Article

Related Posts

Leave a Reply