A Python list is an ordered, mutable collection that can store values of the same or different data types. This tutorial explains how to create, access, modify, search, add, remove, iterate over, slice, copy, and sort Python lists with examples.

What Is a List in Python?

A Python list is an ordered collection of elements. A list can contain integers, strings, floating-point numbers, Boolean values, other lists, or objects of different types in the same collection.

  • Ordered: Elements retain their position.
  • Mutable: Existing elements can be changed after the list is created.
  • Allows duplicates: The same value may occur more than once.
  • Dynamically sized: Elements can be added or removed as needed.
  • Indexable: Each element can be accessed using its position.

Python uses zero-based indexing. The first element is at index 0, the second is at index 1, and the final element is at index len(aList) - 1. Python also supports negative indexes, where -1 refers to the last element.

Python List

Create an Empty Python List

To create an empty list in Python, use the list() constructor.

</>
Copy
aList = list()

You can also create an empty list using empty square brackets.

</>
Copy
aList = []

Both statements create an empty object of type list. Empty square brackets are commonly used because they are concise and easy to read.

Initialize a Python List with Elements

To initialize a Python list, write comma-separated elements inside square brackets and assign the result to a variable.

</>
Copy
aList = [21, 'John', 541, 84.25, True]

The example contains values of several data types. Python does not require every list element to have the same type.

Use the built-in type() function to confirm the data type of aList.

Python Program

</>
Copy
aList = [21, 'John', 541, 84.25, True]
print(type(aList))

The interpreter prints the class of the object to standard output.

Output

<class 'list'>

Access Python List Elements by Index

To access an element in a Python list, write the list variable followed by the element’s index inside square brackets.

</>
Copy
element = aList[index]

Because indexing starts at 0, aList[0] returns the first element, while aList[4] returns the fifth element. Accessing an index outside the valid range raises an IndexError.

Python Program

</>
Copy
aList = [21, 'John', 541, 84.25, True]

element = aList[2]
print(element)

element = aList[4]
print(element)

Output

541
True

aList[2] returns the third element, and aList[4] returns the fifth element.

Access List Elements with Negative Indexes

Negative indexes count backward from the end of a list. Index -1 returns the last element, -2 returns the second-last element, and so on.

</>
Copy
colors = ['red', 'green', 'blue']

print(colors[-1])
print(colors[-2])

Output

blue
green

Modify an Element in a Python List

Python lists are mutable. You can replace the value at a given index by assigning a new value to that position.

</>
Copy
 aList[index] = new_value

In the following program, the element at index 2 is changed from 'cherry' to 'mango'.

Python Program

</>
Copy
aList = ['apple', 'banana', 'cherry', 'orange', 'papaya']

aList[2] = 'mango'

for element in aList:
    print(element)

Output

apple
banana
mango
orange
papaya

Check Whether an Element Is Present in a Python List

Use the in operator to check whether a value is present in a Python list.

</>
Copy
element in aList

The expression returns True when the element is found and False otherwise. Use not in to test that a value is absent.

Python Program

</>
Copy
aList = ['apple', 'banana', 'cherry', 'orange', 'papaya']

element = 'banana'
if element in aList:
    print('The element is present in list.')
else:
    print('The element is not present in list.')

Output

The element is present in list.

Find the Length of a Python List

Pass a list to the built-in len() function to get the number of elements it contains. The function returns an integer.

Python Program

</>
Copy
aList = [21, 541, 84.25]
print(len(aList))

Output

3

Append an Element to a Python List

The list.append() method adds one element to the end of the list. It modifies the existing list and returns None.

Python Program

</>
Copy
aList = [21, 53, 84]
aList.append(96)
print(aList)

Output

3

After 96 is appended, the resulting list is [21, 53, 84, 96].

Add Multiple Elements with extend()

Use extend() when you need to add every element from another iterable to the end of a list.

</>
Copy
numbers = [10, 20]
numbers.extend([30, 40])
print(numbers)

Output

[10, 20, 30, 40]

append([30, 40]) would add the entire nested list as one element, whereas extend([30, 40]) adds 30 and 40 as separate elements.

Insert an Element at a Specific List Index

The list.insert(index, element) method inserts an element at a specified index. Existing elements at that index and later positions are shifted one place to the right.

Python Program

</>
Copy
aList = ['apple', 'banana', 'cherry']

aList.insert(2, 'mango')

for element in aList:
    print(element)

Output

apple
banana
mango
cherry

Remove Elements from a Python List

Python provides several ways to remove list elements:

  • remove(value) removes the first matching value.
  • pop(index) removes and returns the element at an index. Without an index, it removes the last element.
  • del removes an element or a slice by index.
  • clear() removes every element from the list.
</>
Copy
fruits = ['apple', 'banana', 'cherry', 'banana']

fruits.remove('banana')
removed = fruits.pop()

print(fruits)
print(removed)

Output

['apple', 'cherry']
banana

The remove() method raises ValueError if the requested value is absent. Check membership with in first when the value may not exist.

Slice a Python List

List slicing returns selected elements from a list. The general form is aList[start:stop:step]. The start index is included, while the stop index is excluded.

</>
Copy
numbers = [10, 20, 30, 40, 50, 60]

print(numbers[1:4])
print(numbers[:3])
print(numbers[::2])
print(numbers[::-1])

Output

[20, 30, 40]
[10, 20, 30]
[10, 30, 50]
[60, 50, 40, 30, 20, 10]

Iterate over Python List Elements

You can iterate over a Python list using a for loop, a while loop, or other iteration tools. A for loop is usually the clearest choice when you only need each value.

The following program uses a Python For loop to print every list element. Additional statements can be placed inside the loop to process or transform each value.

Python Program

</>
Copy
aList = [21, 53, 84]

for element in aList:
    print(element)

Output

21
53
84

The next program uses a Python While loop and an index to iterate over the same list.

Python Program

</>
Copy
aList = [21, 53, 84]

index = 0
while index < len(aList):
    print(aList[index])
    index += 1

Output

21
53
84

Iterate with Both Index and Value

Use enumerate() when the loop requires both the index and the corresponding list element.

</>
Copy
fruits = ['apple', 'banana', 'cherry']

for index, fruit in enumerate(fruits):
    print(index, fruit)

Output

0 apple
1 banana
2 cherry

Sort a Python List in Ascending or Descending Order

The list.sort() method sorts a list in place. By default, it sorts comparable values in ascending order. Pass reverse=True to sort in descending order.

In the following program, the list of numbers is sorted in ascending order.

Python Program

</>
Copy
aList = [21, 53, 84, 5, 62]

aList.sort()

for element in aList:
    print(element)

Output

5
21
53
62
84

In the following program, the list is sorted in descending order.

Python Program

</>
Copy
aList = [21, 53, 84, 5, 62]

aList.sort(reverse=True)

for element in aList:
    print(element)

Output

84
62
53
21
5

Use the built-in sorted() function instead when you need a new sorted list without changing the original list.

</>
Copy
numbers = [30, 10, 20]
sorted_numbers = sorted(numbers)

print(numbers)
print(sorted_numbers)

Output

[30, 10, 20]
[10, 20, 30]

Copy a Python List Without Sharing the Same Object

Assigning one list variable to another does not create an independent copy. Both variables refer to the same list object, so a change made through one variable is visible through the other.

Use copy(), list(), or a full slice to create a shallow copy.

</>
Copy
original = [10, 20, 30]
copied = original.copy()

copied[0] = 99

print(original)
print(copied)

Output

[10, 20, 30]
[99, 20, 30]

A shallow copy creates a new outer list but still shares references to nested mutable objects. Use copy.deepcopy() when nested objects must also be copied independently.

Create a Python List with a List Comprehension

A list comprehension provides a concise way to build a new list from an iterable. It may also include a condition to filter values.

</>
Copy
squares = [number * number for number in range(1, 6)]
even_squares = [value for value in squares if value % 2 == 0]

print(squares)
print(even_squares)

Output

[1, 4, 9, 16, 25]
[4, 16]

Common Python List Methods

MethodPurpose
append(value)Adds one element to the end.
extend(iterable)Adds all elements from an iterable.
insert(index, value)Inserts an element at a given index.
remove(value)Removes the first matching value.
pop(index)Removes and returns an element.
clear()Removes all elements.
index(value)Returns the index of the first matching value.
count(value)Counts occurrences of a value.
sort()Sorts the list in place.
reverse()Reverses the list in place.
copy()Returns a shallow copy.

Python List Frequently Asked Questions

Can a Python list contain different data types?

Yes. A single Python list can contain strings, numbers, Boolean values, objects, and nested lists. In many applications, however, using elements with a consistent structure makes the code easier to understand and maintain.

What is the difference between a Python list and a tuple?

A list is mutable and uses square brackets, while a tuple is immutable and normally uses parentheses. Choose a list when elements must be added, removed, or changed after creation.

What happens when a list index is out of range?

Python raises an IndexError when code tries to access an index that does not exist. Valid positive indexes range from 0 through len(aList) - 1.

Does append() add one element or multiple elements?

append() always adds its argument as one element. To add multiple individual elements from another iterable, use extend().

Summary of Python List Operations

In this Python Tutorial, we learned that a list is an ordered and mutable collection. We created empty and initialized lists, accessed values with positive and negative indexes, modified and searched elements, added and removed values, sliced and copied lists, iterated with loops, created list comprehensions, and sorted list elements.