TutorialAce

Python | Java | MySQL | Excel

Python Tutorial

Python List with Examples

How to Use the Python List

In this tutorial, we'll learn what is a list in Python, how to create, copy, sort, and delete a list, add and remove elements to and from a list, and count the number of elements in a list.

What is a List in Python?

A list is a collection of elements and contains duplicate elements. It's one of the built-in collection data types and mostly used in Python. Unlike sets, lists can contain duplicate values.

Creating a List

List literals are written within square brackets [ ].

The following code illustrates how to create a list of elements:

fruits = ["Orange", "Apple", "Grape", "Melon", "Grapefruit"]
numbers = [2, 3, 2, 3, 10, 4]
print (fruits)
print (numbers)

Result:

['Orange', 'Apple', 'Grape', 'Melon', 'Grapefruit']
[2, 3, 2, 3, 10, 4]

Accessing Item in a List

To access elements in a list, we can use the index operator []. Index number starts from 0 (zero).

For example, let's say we have the following list:

fruits = ["Apple", "Orange", "Grape", "Melon"]

To access the third element in the list, we use the following code:

fruit = fruits[2]

Adding Items

To add an item to a list, we use the append() method.

Note that a new item added to a list is the last item of the list.

Adding an Item to a List

The following example illustrates how to add one item to a list:

students = ["Student A", "Student B", "Student C", "Student D"]
students.append("Student E")
print (students)

Result:

['Student A', 'Student B', 'Student C', 'Student D', 'Student E']

Inserting an Item to a List

We can add an item to a list at the specified index using insert() method.

The following example illustrates how to add 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)

Result:

['Student A', 'Student F', 'Student B', 'Student C', 'Student D', 'Student E']

Removing Items

There are several ways to remove item from a list.

Removing the Specified Item

To remove the specified item from a list, we use the remove() method.

The following example illustrates how to remove an item from a list:

vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
vegetables.remove("cabbage")
print (vegetables)

Result:

['onion', ' broccoli ', 'cauliflower', 'cabbage']

Note that the remove() method removes the first occurrence of items.

Removing at the Specified Index

To remove an item at the specified index, we can either pop() or del keyword.

The following example illustrates how to remove an item at the third position of a list:

vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
vegetables.pop(2)
print (vegetables)

or:

vegetables = ["cabbage", "onion", " broccoli ", "cauliflower", "cabbage"]
del vegetables[2]
print (vegetables)

Result:

['cabbage', 'onion', 'cauliflower', 'cabbage']

Note that the pop() method removes the last item of a list if index is not specified. And the del keyword can be used to delete a list completely.

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)

Result:

['cabbage', 'onion', ' broccoli ', 'cauliflower']

The following example illustrates how to delete a list completely:

numbers = [2, 4, 5, 6, 3, 5]
del numbers

Copying a List

To copy a list, we can use the copy() or list() method.

The following example illustrates how to copy a list using the copy() method:

vegetables = ["garlic", "onion", " broccoli ", "cauliflower", "cabbage", "chili pepper"]
vegetables2 = vegetables.copy();
print (vegetables2)

Result:

['garlic', 'onion', ' broccoli ', 'cauliflower', 'cabbage', 'chili pepper']

The following example illustrates how to copy a list using the list() method:

vegetables = ["garlic", "onion", " broccoli ", "cauliflower", "cabbage", "chili pepper"]
vegetables2 = list(vegetables);
print (vegetables2)

Result:

['garlic', 'onion', ' broccoli ', 'cauliflower', 'cabbage', 'chili pepper']

Note that we cannot copy a list like this:

vegetables2 = vegetables

Because vegetables2 is only a reference to vegetables. This means that, changes made in vegetables will also be made in vegetables2.

Emptying a List

To empty a list, we use the clear() method.

The following example how to empty a list using the clear() method:

sports = ["Soccer", "Tennis", "Volleyball", "Basketball"]
sports.clear()
print(sports)

Result:

[]

Sorting a List in Python

We can use the sort() method to sort a list.

The following example illustrates how to sort a list ascending:

vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
vegetables.sort();
print (vegetables)

Result:

[' broccoli ', 'cabbage', 'cabbage', 'cauliflower', 'garlic', 'onion']

The following example illustrates how to sort a list descending:

vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
vegetables.sort(reverse = True);
print (vegetables)

Result:

['onion', 'garlic', 'cauliflower', 'cabbage', 'cabbage', 'broccoli']

Counting the Number of Elements

To count the number of occurrences of a given item in the list, we use the count() method.

The following example illustrates how to count the number of occurrences of Apple in the list:

fruits = ["Grape", "Apple", "Orange", "Apple", "Melon"]
count = fruits.count("Apple")
print("The number of occurrences of Apple is", count)

Result:

The number of occurrences of Apple is 2

To count the number of elements in a list, we can use len() method.

The following examples illustrates how to count 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)

Result:

The number of elements is 10

Iterating a List in Python

We can iterate over a list using the For loop statement.

The following example illustrates how to iterate over a list using the For loop statement:

vegetables = ["garlic", "cabbage", "onion", "broccoli", "cauliflower", "cabbage"]
for vegetable in vegetables:
print(vegetable)

Result:

garlic
cabbage
onion
broccoli
cauliflower
cabbage

In this tutorial, we've learned what a list in Python, how to create, copy, delete, and sort a list, how add and remove elements to and from a list, and how to count the number of elements in a list.

Leave a Reply