Pylerno
English

Lists

Store related values in order and update a selected item.

On this page

The concept

A list is an ordered collection that allows repeated values and supports mutation. Use it when position matters or when you need to accumulate items. Lists support the same basic indexing and slicing notation introduced for strings, but list elements can be replaced.

A slice returns a new outer list; deeper copying is a separate topic. Keep the members of a list conceptually related, even though Python allows different types. A list of fruit names is easier to reason about than a mixture of unrelated fields with undocumented positions.

Worked example

python
fruits = ["apple", "banana", "orange"]
print(fruits[0], fruits[-1])
fruits[1] = "pomegranate"
print(fruits[:2])
print(len(fruits), "apple" in fruits)
for fruit in fruits:
    print(fruit)
empty_list = []
print(bool(empty_list))

Trace the result

  1. The first and last items are read with 0 and -1.
  2. Replacing fruits[1] changes the existing list, unlike constructing a new string.
  3. Membership checks values, len counts items, and an empty list is false-valued.
text
apple orange
['apple', 'pomegranate']
3 True
apple
pomegranate
orange
False
Line-by-line walkthrough

Line 1: fruits = ["apple", "banana", "orange"]

Evaluate ['apple', 'banana', 'orange'], then bind or update fruits with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 2: print(fruits[0], fruits[-1])

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 3: fruits[1] = "pomegranate"

Evaluate 'pomegranate', then bind or update fruits[1] with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 4: print(fruits[:2])

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print(len(fruits), "apple" in fruits)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 6–7: for fruit in fruits:

Obtain each item from fruits and bind it to fruit before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Line 7: print(fruit)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: empty_list = []

Evaluate [], then bind or update empty_list with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 9: print(bool(empty_list))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

The last index of a three-item list is 2, not 3. Always distinguish a position from a count.

Check your understanding

What does this program print?

python
a = ["tea", "milk"]
a.append("tea")
print(len(a))
Choose one answer

Search documentation

Share this page