What is a List in Python and How to Use It
In this tutorial, you'll learn what a list is in Python and how to use it. You'll learn how to create, copy, sort, add, and remove elements from and to a list. And, you also learn how to count the number of items in the list.
What is a List in Python?
A list is a built-in collection data type that is a collection of elements and can contain duplicate values.
Creating a List
In Python, list literals are written within square brackets [ ].
The following code illustrates an example of how to create a list of elements:
fruits = ["Orange", "Apple", "Grape", "Melon", "Grapefruit"]
numbers = [2, 3, 2, 3, 10, 4]
print (fruits)
print (numbers)
Output:
['Orange', 'Apple', 'Grape', 'Melon', 'Grapefruit']
[2, 3, 2, 3, 10, 4]
Accessing Items in a List
To access elements in a list, you can use the index operator []. Index number starts from 0 (zero).
For example, let's say you have the following list:
fruits = ["Apple", "Orange", "Grape", "Melon"]
And you want to access the third element in the list, and then you can write as follows:
fruit = fruits[2]
Adding Items
It's easy to add an item to a list. Python provides the list append() method that allows you to add an item to the list.
To add an item to a list, you use the append() method as follows:
list.append(item)
Note that a new item added to the list is the last item of the list.
Example of Adding an Item to a List
Let's take a look at an example:
students = ["Student A", "Student B", "Student C", "Student D"]
students.append("Student E")
print (students)
First, we have created a list of 4 items, Student A, Student B, Student C, and Student D. And then we added item Student E to the last index of the list using the append() method.
Output:
['Student A', 'Student B', 'Student C', 'Student D', 'Student E']
Inserting Items
You can add an item to a list at the specified index using the list insert() method.
To insert an item to a list, you use the following code:
list.insert(index, item)
Example of Inserting an Item to a List
The following code illustrates an example of how to insert a new item at the second position of the list:
students = ["Student A", "Student B", "Student C", "Student D", "Student E"]
students.insert(1, "Student F")
print (students)
Here, the item Student F, inserted at position 2 of the list.
Output:
['Student A', 'Student F', 'Student B', 'Student C', 'Student D', 'Student E']
Removing Items
There are several ways to remove an item from a list.
Removing the Specified Item
To remove the specified item from a list, you use the remove() method as follows:
list.remove(item)
Note that if there are duplicate items, the list method() removes the first occurrence.
The following program illustrates an example of how to remove an item from a list:
vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
vegetables.remove("cabbage")
print (vegetables)
Output:
['onion', ' broccoli ', 'cauliflower', 'cabbage']
As you can see, the first occurrence of cabbage removed since there are two cabbages in the list.
Removing at the Specified Index
To remove an item at the specified index, you can either pop() or del keyword.
If an index is not specified, pop() removes the last item of the list.The del keyword also can be used to delete a list completely.
To remove an item using the list pop() method, you use the following code:
list.pop(index)
or
list.pop()
Example of Using the List pop() Method
The following code illustrates an example of how to remove an item at position 3 of the list:
vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
vegetables.pop(2)
print (vegetables)
To remove an item using the del keyword, you use the following code:
del list[index]
Example of Using the del Keyword
The following code illustrates an example of how to remove an item at position 3 of the list using the del keyword:
vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
del vegetables[2]
print (vegetables)
Output:
['cabbage', 'onion', 'cauliflower', 'cabbage']
The following example illustrates how to remove the last item of a list using the pop() method:
vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
vegetables.pop()
print (vegetables)
Output:
['cabbage', 'onion', ' broccoli ', 'cauliflower']
The following code completely deletes a list:
numbers = [2, 4, 5, 6, 3, 5]
del numbers
Copying a List
To copy a list, you can use the copy() or list() method.
To copy a list using the copy() method, you use the following code:
new_list = old_list.copy()
Example:
vegetables = ["garlic", "onion", " broccoli ", "cauliflower", "cabbage", "chili pepper"]
vegetables2 = vegetables.copy();
print (vegetables2)
Output:
['garlic', 'onion', ' broccoli ', 'cauliflower', 'cabbage', 'chili pepper']
To copy a list using the list() method, you use the following code:
new_list = list(old_list)
Example:
vegetables = ["garlic", "onion", " broccoli ", "cauliflower", "cabbage", "chili pepper"]
vegetables2 = list(vegetables);
print (vegetables2)
Output:
['garlic', 'onion', ' broccoli ', 'cauliflower', 'cabbage', 'chili pepper']
Note that you cannot copy a list like this:
vegetables2 = vegetables
Because vegetables2 is only a reference to vegetables. So, changes made in vegetables will be made in vegetables2 as well.
Emptying a List
To empty a list, you use the clear() method as follows:
list.clear()
Example:
sports = ["Soccer", "Tennis", "Volleyball", "Basketball"]
sports.clear()
print(sports)
Output:
[]
Sorting a List in Python
The simple way to sort a list is to use the list sort() method.
list.sort(key=None, reverse=False)
The following code illustrates an example of how to sort a list in ascending order:
vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
vegetables.sort();
print (vegetables)
Output:
[' broccoli ', 'cabbage', 'cabbage', 'cauliflower', 'garlic', 'onion']
The following code illustrates an example of how to sort a list in descending order:
vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
vegetables.sort(reverse = True);
print (vegetables)
Output:
['onion', 'garlic', 'cauliflower', 'cabbage', 'cabbage', 'broccoli']
Counting the Number of Elements
To count the number of occurrences of a given item in the list, you use the count() method.
list.count(item)
The following code counts the number of times that Apple appears in the list:
fruits = ["Grape", "Apple", "Orange", "Apple", "Melon"]
count = fruits.count("Apple")
print("The number of occurrences of Apple is", count)
Output:
The number of occurrences of Apple is 2
And, to count the number of elements in a list, you can use the len() method.
The following code counts the number of items in the list:
numbers = [2, 3, 2, 4, 10, 9, 8, 7, 2, 6]
length = len(numbers)
print("The number of elements is", length)
Output:
The number of elements is 10
Iterating a List in Python
You can iterate over a list using the For loop statement.
The following code illustrates an example of how to iterate over a list using the For loop statement:
vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
for vegetable in vegetables:
print(vegetable)
Output:
garlic
cabbage
onion
broccoli
cauliflower
cabbage
In this tutorial, you've learned what a list is in Python and how to use it. You've learned to create, copy, sort, add, and remove elements from and to a list. And, you have also learned how to count the number of items in the list.