Beginner · 10 min
Your first list
Keep several values in a list and access them by position.
Group related values
A list uses square brackets with commas between items. Use len() to count its items. Lists keep items in order.
items = ["bread", "milk", "eggs"]
print(len(items))Output
3
Read an item by index
List positions start at zero. Index 0 selects the first item and -1 selects the last. An index outside the list raises an error.
items = ["bread", "milk", "eggs"]
print(items[0])
print(items[-1])Output
bread eggs
Your first list
- Number the list positions starting at zero.
- Find the first and last items.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Read a shopping list
Use the supplied list. Store its first item in first and its length in count, then display first and count on separate lines.
Use items[0] for the first item and len(items) for the count.