Collections

Lists

Lists are ordered, mutable collections.

Create a list with square brackets. Because lists are mutable, append() changes this same list object.

Source

numbers = [3, 1, 4]
numbers.append(1)

print(numbers)

Output

[3, 1, 4, 1]
314+1.append
Lists are mutable sequences. `.append` extends the same list object — no new list is created.

Use indexes to read positions. Negative indexes are convenient for reading from the end.

Source

print(numbers[0])
print(numbers[-1])

Output

3
1

Use sorted() when you want an ordered copy and still need the original order afterward.

Source

print(sorted(numbers))
print(numbers)

Output

[1, 1, 3, 4]
[3, 1, 4, 1]

Notes

See also

Run the complete example

Example code

Expected output

[3, 1, 4, 1]
3
1
[1, 1, 3, 4]
[3, 1, 4, 1]

Execution time appears here after you run the example.