How to Create a List in Python
What you’ll build or solve
You’ll create a list, empty or pre-filled, add items, read items by index, update or remove items, and loop through the list.
When this approach works best
This approach works best when you need:
Learn Python on Mimo
- An ordered collection, like a set of steps in a workflow or names to display in order.
- A container you can change as your program runs, like adding new items or removing completed ones.
- A simple way to loop over many values, like calculating totals from a series of numbers.
Avoid this approach when:
- You need fast membership checks and no duplicates. Use a
set. - You need key-value lookup. Use a
dict.
Prerequisites
- Python 3 installed
- You know how to:
- Assign a variable:
x = 1 - Print a value:
print(x) - Run a few lines of Python in a file or an interactive shell
- Assign a variable:
Step-by-step instructions
1) Create a list
Option A: start with values
colors= ["blue","green","red"]
numbers= [10,20,30]
print(colors)
print(numbers)
Option B: start empty
colors= []
print(colors)# []
2) Add items to a list
Add one item with append():
colors= ["blue","green"]
colors.append("red")
print(colors)# ['blue', 'green', 'red']
Add several items with extend():
CSS
colors= ["blue"]
colors.extend(["green","red"])
print(colors)# ['blue', 'green', 'red']
What to look for:
append() adds one element. extend() adds each element from another iterable.
3) Read items by index
Indexes start at 0. Negative indexes count from the end.
colors= ["blue","green","red"]
print(colors[0])# blue
print(colors[1])# green
print(colors[-1])# red
If you are not sure an index exists, check the length:
i=3
ifi<len(colors):
print(colors[i])
else:
print("index out of range")
4) Update items
Update an item by index:
colors= ["blue","green","red"]
colors[1]="yellow"
print(colors)# ['blue', 'yellow', 'red']
Insert an item at a specific position:
colors.insert(1,"purple")
print(colors)# ['blue', 'purple', 'yellow', 'red']
5) Remove items
Remove by value with remove():
colors= ["blue","green","red"]
colors.remove("green")
print(colors)# ['blue', 'red']
Remove by index with pop():
colors= ["blue","green","red"]
last=colors.pop()
print(last)# red
print(colors)# ['blue', 'green']
Remove a specific position with pop(index):
colors= ["blue","green","red"]
middle=colors.pop(1)
print(middle)# green
print(colors)# ['blue', 'red']
What to look for:
remove() fails if the value is missing.
pop() fails if the index is out of range.
6) Loop through items
Loop over values:
CSS
colors= ["blue","green","red"]
forcolorincolors:
print(color)
Loop with index and value:
fori,colorinenumerate(colors):
print(i,color)
Examples you can copy
1) Build a list by adding items
shopping= []
shopping.append("milk")
shopping.append("bread")
shopping.append("eggs")
print(shopping)
2) Convert a string into a list of words
sentence="Python lists are useful"
words=sentence.split()
print(words)# ['Python', 'lists', 'are', 'useful']
3) Update and remove items from a to-do list
LUA
todo= ["email Sam","pay rent","book dentist"]
todo[0]="email Alex"# update
todo.remove("pay rent")# remove by value
done=todo.pop()# remove last item
print("done:",done)
print("left:",todo)
Common mistakes and how to fix them
Mistake 1: Creating a tuple by accident
You might write:
items= (1,2,3)
print(type(items))
Why it breaks:
Parentheses create a tuple, not a list, so you can’t change it the same way.
Correct approach:
items= [1,2,3]
items.append(4)
print(items)# [1, 2, 3, 4]
Mistake 2: Using an index that does not exist
You might write:
colors= ["blue","green","red"]
print(colors[3])
Why it breaks:
Valid indexes are 0 to len(colors) - 1.
Correct approach:
print(colors[-1])# last item
i=3
ifi<len(colors):
print(colors[i])
Mistake 3: Calling remove() on a value that is not in the list
You might write:
colors= ["blue","green","red"]
colors.remove("yellow")
Why it breaks:
remove() raises an error when the value is missing.
Correct approach:
colors= ["blue","green","red"]
if"yellow"incolors:
colors.remove("yellow")
Troubleshooting
If you see IndexError: list index out of range, print len(your_list) and confirm your index is less than the length. Use -1 for the last item.
If you see ValueError: list.remove(x): x not in list, check with if x in your_list: before calling remove().
If you see AttributeError: 'list' object has no attribute 'add', use append() to add one item or extend() to add many.
If you see a tuple type when you expected a list, check for parentheses () instead of brackets [].
If your “list of numbers” prints like ['1', '2'], you have strings. Convert them with int() when you create the list.
Quick recap
- Create a list with
[]or with values like["a", "b"]. - Add one item with
append(), add many withextend(). - Read items with
list[index], and use1for the last item. - Update with
list[index] = value, insert withinsert(). - Remove by value with
remove(), or by index withpop(). - Loop with
for item in listorenumerate(list)when you need indexes.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot