Nested Lists
So far, all of the elements in our lists have been of the same type: strings, numbers, etc. However, there are no restrictions on the types of elements in a list. Elements of a list can be any type you want, you can also mix and match different types of elements in a list. For example:
mixed_up = ['apple', 3, 'oranges', 27, [1, 2, ['alpha', 'beta']]]
or a more useful example:
junoon = [['Ali', 1970], ['Brian', 1963], ['Salman', 1963]]
You can use indexing again on the list that results to obtain an inner element:
print(junoon) # -> [['Ali', 1970], ['Brian', 1963], ['Salman', 1963]]
print(junoon[2]) # -> ['Salman', 1963]
print(junoon[2][0]) # -> Salman
Quiz 3.3
Given the variable countries defined as:
countries = [
['China', 'Beijing', 1393],
['Pakistan', 'Islamabad', 210],
['Turkey', 'Ankara', 82],
['Finland', 'Helsinki', 6]
]
Each list contains the name of a country, its capital, and its approximate population in millions. Write code to print out the capital of Turkey.
Answer
print(countries[2][1])
Quiz 3.4
Consider countries
variable defined in Quiz 3, What multiple of Finland's population is the population of China? To solve this, you need to divide the population of China by the population of Finland.
Answer
print(countries[0][2] / countries[3][2])