By Hemanta Sundaray on 2022-01-09
A list is one of the many built-in data structures that allows us to work with a collection of data in sequential order.
We can store the list of top three mountains by height in a list as shown below:
mountains = ["Mauna Kea", "Mt. Everest", "K2"]
Notice that:
We can combine multiple data types in one list. For example, the following list contains both a string and an integer:
mixed_list = ["Hemanta", 12]
Lists can contain any data type in Python! For example, this list contains a string, integer, boolean and float.
mixed_list = ["Hemanta", 12, True, 24.8]
We can create an empty list as shown below:
empty_list = [ ]
We can use the popular list method .append() to add an element to the end of a list.
name = ["Hemanta", "Kumar"]
name.append("Sundaray")
print(name)
# ['Hemanta', 'Kumar', 'Sundaray']
When we want to add multiple items to a list, we can use + to combine two lists.
Note that we can only use
+with other lists.
first_name = ["Hemanta"]
last_name = ["Sundaray"]
full_name = first_name + last_name
print(full_name)
# ['Hemanta', 'Sundaray']
We can select a single element from a list by using square brackets ([]) and the index of the list item.
Consider the list below:
energy_drinks = ["Red Bull", "Monster", "Bang"]
If we want to access the third element of the list above, we would use print(energy_drinks[2]).
energy_drinks = ["Red Bull”, “Monster”, “Bang”]
print(energy_drinks[2])
# Bang
We can use the index -1 to select the last item of a list, even when we don’t know how many items are there in the list.
Consider the following list with 5 items:
chocolate = ["Nestle”, “Milka”, “Ghirardelli”, “Cadbury”, “Godiva”]
If we select the -1 index, we get the final element"
print(chocolate[-1])
# Godiva
We can change a value in a list by reassigning the value using the specific index.
Consider the following list:
candies = ["Wrigley”, “Ferrero”, “Mondelez”, “Meizi”, “Hershey”]
We can change the value of the third item (index 2) as shown below:
candies[2] = "Lindt"
print(candies)
# ['Wrigley', 'Ferrero', 'Lindt', 'Meizi', 'Hershey']
We can remove elements in a list using the remove() Python method.
Below, we have a list of the top three car brands in Korea:
brands = ["Hyundai", "Kia", "GM"]
We can remove Kia by using the remove() method:
brands = ["Hyundai", "Kia", "GM"]
brands.remove("Kia")
print(brands)
# ['Hyundai', 'GM']
We can also use .remove() on a list that has duplicate elements:
Only the first instance of the matching element is removed:
brands = ["Hyundai", "Kia", "Kia", "GM"]
brands.remove("Kia")
print(brands)
# ['Hyundai', 'Kia', 'GM']
Lists can contain other lists! We will commonly refer to these as two-dimensional lists.
country_capital = [["India", "New Delhi"], ["China", "Beijing"], ["South Korea", "Seoul"], ["Japan", "Tokyo"]]
Two-dimensional lists can be accessed similar to their one-dimensional counterparts. Instead of providing a single pair of brackets, we will use an additional set for each dimension past the first.
If we wanted to access China’s capital:
# Access the sublist at index 1, and then access the 1st index of that sublist.
china_capital = country_capital[1][1]
print(china_capital)
# Beijing
The following list stores the floor numbers of different clothing lines:
floor_line = [["First", "Menswear"], ["Second", "Womenswear"], ["Third", "Kidswear"]]
To change a value in a two-dimensional list, reassign the value using the specific index.
floor_line[0][1] = "Accessories"
print(floor_line)
# [['First', 'Accessories'], ['Second', 'Womenswear'], ['Third', 'Kidswear']]