π
Python Lists
Ordered, changeable collections
Reward
+60 XP
Creating lists
Lists are ordered collections that can hold any type. Use square brackets.
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True]
print(fruits)Accessing and modifying
Access items by index. Lists are mutable β you can change items in place.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
fruits[1] = "mango"
print(fruits)List methods
append(), insert(), remove(), pop(), sort(), reverse(), len().
nums = [3, 1, 4, 1, 5]
nums.append(9)
nums.sort()
print(nums)Try it yourself
Run the example below. Edit it and see what changes.
main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)
Common mistakes
- β Index out of range β accessing an index beyond the list length.
- β Confusing append() (adds one item) with extend() (adds many).
- β Forgetting that sort() modifies the list in place and returns None.
