Growing Your List โ
Lists aren't frozen โ you can add to them while your program runs!
.append() sticks a new item onto the end of a list:
squad = ["Ana"]
squad.append("Bo")
squad.append("Cy")
print(squad) # โ ['Ana', 'Bo', 'Cy']
print(len(squad)) # โ 3
Read it out loud as "squad, append Bo" โ the list name, then a dot, then the thing you want to do to it.
You can even start with a totally empty list and fill it up:
collected = []
collected.append("first treasure!")
Your mission
You're packing for an adventure.
- Make a list called
inventorywith 2 starting items - Use
.append()three times to add 3 more items - Print the finished inventory
- Print how many items you're carrying with
len()
Your code
Press โถ Run to see what your code does!