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 the previous tutorial, 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

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

Duplicate Item

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

Data Type of list.

If you want to know the data type of the 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 an index number, which that means with the help of an 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 a specific range, Then you can use slicing.

In Python slicing, you have to define the start and end index numbers for the 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 slicing, the end index number element is not included in the result, for example in the above result apple index number is 5 who not included 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:

Sometimes you will need to change specific items in the list, you can use the index numbers to change values.

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 a loop. To display items from the 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 the 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 a new item to the 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 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 a specific index item from the list, use the pop() method.

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

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

del keyword use in Python list:

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

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

b. If you want to delete the complete list, use the 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()The 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 extends() method is used to add the elements of a list to the end current list.
insert()The 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()The python pop() method is used to remove the element from a specific position.
reverse()Reverse the order of the list.
sort()The 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.

Thanks for your valuable time 👏👏👏

Python Pass Statement Tutorial
Python Operators Tutorial

Related Posts