Learn how to store multiple values and variables in one place!
Storing the value of your favorite song in a variable is a pretty simple process in Python. We can simply set a variable equal to the value of the song name:
But what if you were asked to list your top ten favorite songs? Top 100?? It would take a long time to write out each song in a different variable, not to mention it would be hard to remember the name of each variable and which variable corresponded with the correct song. Luckily, Python is equipped with a way to store multiple values in a single data structure, so all your favorite songs can be found in one place!
Lists are one example of a data structure used in Python to help organize and store data efficiently and effectively. Lists are created by using square brackets:
We can either start by creating en empty list, like the example above, or add values to it from the get go:
Values stored in a list are separated by commas, and maintain the same order that they are written in.
Values can be added to a list by using the append
method. append
will add a value to the end of a list:
To add a value to a specific place in the list, we can make use of the insert
method. insert
will add a value to a specified index in that list:
Just like Strings, each item in a list is designated an index, which indicates its location within the list. The first item in a list is located at index 0, and each subsequent value is located at one index higher than that.
To access a specific value within an index, we just need to refer to its index value:
FeedBuzz is an online company that creates surveys. They want to collect information on the best websites that people can’t live without. To help them get started, create a variable listOfWebsites
that stores an empty list. Then, add 4 websites that you think you can’t live without. From that list of 4, print the one website that’s most important by indexing into the list and pulling out the website name.
List items can also be changed or removed. To change a list item, we refer to the index that a value is located at, and set it equal to something else:
To remove an item completely from a list, we can use the remove
method to remove a specific item, or the pop
method to remove a specific index:
If no index is provided to the pop
method, then the last item in the list will be removed.
It’s also important to note that remove
will only remove the first instance that an item appears in a list, not all instances of the same item.
An airline accidentally added the same passenger to their flight a few too many times. Using the different methods learned in this tutorial, alter the list so that only one instance of the name “Jojo” appears in the list. Use three different methods to change or remove the name from the list.
In addition to the methods already discussed, there are several other handy methods that can be used to manipulate lists and list items:
len()
- returns the length of the list
index(value)
- returns the index of the first element of the specified value
reverse()
- reverses the order of a list
sort()
- sorts a list
Think you’ve got a hang on lists? See how well you do on this quiz:
Look at the following program:
my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... # ... # ... print(my_list)
Pick the code that results in the following output:
['apples', 'bananas', 'grapes', 'oranges', 'pineapples']
Look at the following program:
my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"] # You pick the code that goes here... # ... # ... print(my_list)
Pick the code that results in the following output:
['pineapples', 'oranges', 'grapes', 'apples']