Intermediate âĸ Python
Lists
đ Creating a List
A list allows us to store multiple values in one variable. Lists are written using square brackets, and individual values are separated by commas.
fruits = ["Apple", "Banana", "Mango"]
print(fruits)['Apple', 'Banana', 'Mango']
đ Everything in Lists
Store multiple pieces of information together. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.
đ Creating a List
A list allows us to store multiple values in one variable. Lists are written using square brackets, and individual values are separated by commas.
fruits = ["Apple", "Banana", "Mango"] print(fruits)đĸ List Indexing
Each item in a list has a position called an index. Python starts counting indexes from 0, so the first item is at index 0, the second item is at index 1, and so on.
fruits = ["Apple", "Banana", "Mango"] print(fruits[0]) print(fruits[1])â Adding Items to a List
We can add an item to the end of a list using the append() method. This is useful when we want our list to grow while a program is running.
fruits = ["Apple", "Banana"] fruits.append("Mango") print(fruits)âī¸ Changing List Items
List items can be changed by using their index. We assign a new value to the position we want to change.
fruits = ["Apple", "Banana", "Mango"] fruits[1] = "Orange" print(fruits)đ Counting Items with len()
The len() function tells us how many items are inside a list. This is useful when we want to know how big a list is, such as how many players are in a game.
fruits = ["Apple", "Banana", "Mango"] print(len(fruits))đī¸ Removing Items
We can take items out of a list in two main ways. remove() deletes the first item that matches a value. pop() takes the last item out of the list and gives it back to us, so we can store it in a variable.
fruits = ["Apple", "Banana", "Mango", "Grape"] fruits.remove("Banana") print(fruits) last = fruits.pop() print(last) print(fruits)đ Looping Over a List
A for loop can visit every item in a list, one after another. This lets us do something with each item, such as printing it, without writing a separate line for every item.
colors = ["red", "green", "blue"] for color in colors: print(color)đ Checking with in
The in keyword checks whether a value is inside a list. It gives True if the value is found and False if it is not. This is often used together with an if statement.
fruits = ["Apple", "Banana", "Mango"] print("Mango" in fruits) print("Grape" in fruits)đ¤ Sorting a List
The sort() method puts the items in a list in order. Numbers are sorted from smallest to largest, and words are sorted in alphabetical order. sort() changes the list itself.
numbers = [5, 2, 9, 1] numbers.sort() print(numbers) names = ["Zoe", "Adam", "Mia"] names.sort() print(names)âī¸ Slicing a List
Slicing lets us take a part of a list. letters[0:2] gives the items from index 0 up to, but not including, index 2. We can also use a negative index such as -1 to get the last item.
letters = ["a", "b", "c", "d", "e"] print(letters[0:2]) print(letters[1:4]) print(letters[-1])
