Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A Python list is a mutable, ordered sequence: you can access items by position, change the sequence, and process its contents. Use square brackets to create one, then choose the operation that matches your task—such as append() to add one item or extend() to add items from an iterable.

What is a Python list?

A list stores items in order and can be changed after it is created. For example:

colors = ["red", "blue"]

List indexes start at zero, so colors[0] is "red" and colors[1] is "blue". You can also use slices to work with a range of items. Python’s sequence tutorial explains indexing, slicing, and how lists relate to other sequence types.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read, update, and loop over list items

Read an item by its index, or assign a new value at that index to update the list:

colors = ["red", "blue"]
print(colors[0])  # red

colors[1] = "green"
print(colors)     # ['red', 'green']

Use a for loop when you want to handle each item in turn:

for color in colors:
    print(color)

Add items: append or extend?

Use append(item) to add one item at the end. If that item is itself a list, the inner list remains a single nested item. Use extend(iterable) to add the iterable’s contents as separate items.

colors = ["red", "blue"]
colors.append(["green", "yellow"])
print(colors)  # ['red', 'blue', ['green', 'yellow']]

colors = ["red", "blue"]
colors.extend(["green", "yellow"])
print(colors)  # ['red', 'blue', 'green', 'yellow']

To insert one item before a particular position, use insert(index, item):

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
colors.insert(1, "green")
print(colors)  # ['red', 'green', 'blue']

Remove items by value, index, or slice

Choose a removal operation based on how you identify the item and whether you need the removed value.

  • remove(value) removes the first item equal to value. It raises ValueError if the value is absent; repeated matches after the first remain.
  • pop(index) removes and returns the item at that index. If you omit the index, it removes and returns the last item.
  • del removes an item by index or a range by slice, without returning the removed item.
numbers = [10, 20, 20, 30]
numbers.remove(20)
print(numbers)  # [10, 20, 30]

last = numbers.pop()
print(last)     # 30

del numbers[0]
print(numbers)  # [20]

Sort or reverse a list without losing track of the result

Methods that modify a list, including sort(), reverse(), and append(), change the existing list and return None. Do not assign the result of one of these calls back to the list variable:

numbers = [3, 1, 2]
numbers.sort()
print(numbers)  # [1, 2, 3]

Writing numbers = numbers.sort() instead replaces numbers with None. If you need a sorted result while keeping the original list unchanged, use the built-in sorted() function:

numbers = [3, 1, 2]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # [1, 2, 3]
print(numbers)         # [3, 1, 2]

Make a shallow copy

copy() returns a shallow copy of a list. The outer list is new, but nested mutable items are not recursively copied.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
original = [[1, 2], [3, 4]]
copy_of_original = original.copy()

Use a list as a stack, but choose deque for a queue

Stack: add and remove at the end

A list works naturally as a last-in, first-out stack: add an item with append() and take the most recently added item with pop().

stack = ["first"]
stack.append("second")
top = stack.pop()
print(top)    # second
print(stack)  # ['first']

Queue: add and remove at both ends

A list can act as a queue, but removing from the front requires shifting the remaining items, which is slow as the queue grows. For efficient operations at both ends, use collections.deque:

from collections import deque

queue = deque(["first", "second"])
queue.append("third")
next_item = queue.popleft()
print(next_item)  # first
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Build a new list with a comprehension

A list comprehension constructs a new list by applying an expression to each item, optionally including only items that meet a condition. Start with a loop to see the same transformation in two forms:

squares = []
for number in range(5):
    squares.append(number * number)

print(squares)  # [0, 1, 4, 9, 16]

The comprehension expresses that transformation more compactly:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
squares = [number * number for number in range(5)]

Add an if clause to select items. This example keeps only even numbers before squaring them:

even_squares = [number * number for number in range(10) if number % 2 == 0]
print(even_squares)  # [0, 4, 16, 36, 64]

See the Python tutorial’s list-comprehension section for further examples.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.