Lists: Many Values in One Place

What is a list?

A list holds many values in one variable, in order. Write it with square brackets [ ] and separate the items with commas.

Think of it like this: A list is like a shopping list: one piece of paper, many items, in a particular order.

fruits = ["apple", "banana", "mango"]
marks = [78, 92, 65, 88]
print(fruits)
print(marks)

Output

['apple', 'banana', 'mango']
[78, 92, 65, 88]

A list can hold any type, even a mix: ["Asha", 21, True]. An empty list is [].

Getting items

Lists use indexes just like strings – the first item is at index 0:

fruits = ["apple", "banana", "mango", "grapes"]
print(fruits[0])
print(fruits[-1])
print(fruits[1:3])
print(len(fruits))

Output

apple
grapes
['banana', 'mango']
4

Is it in the list?

fruits = ["apple", "banana", "mango"]
print("mango" in fruits)
print("kiwi" in fruits)

Output

True
False

Remember: Strings and lists both use [index], slicing, len() and in. Learn them once, use them for both!

HOME LEARN COMMUNITY DASHBOARD