Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Nesting

Give Feedback

In this lesson, you’ll explore how lists can be nested. You’ve seen that an element in a list can be any sort of object. That includes another list. A list can contain sublists, which in turn can contain sublists themselves, and so on to arbitrary depth:

>>>
>>> x = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x[0]
'a'
>>> print(x[0], x[2], x[4])
a g j
>>> x[1]
['bb', ['ccc', 'ddd'], 'ee', 'ff']
>>> x[1][0]
'bb'
>>> x[1][1]
['ccc', 'ddd']
>>> x[1][2]
'ee'
>>> x[1][1][1]
'ddd'
>>> x[3][0]
'hh'
>>> x[-2][-1]
'ii'
>>> x[-4][-3][-2]
'ccc'

>>> x
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> len(x)
5
>>> 'ddd' in x
False
>>> 'ddd' in x[1]
False
>>> 'ddd' in x[1][1]
True

Hello Sir, what are some good use cases of NESTED LISTS?

Chris Bailey RP Team on Dec. 31, 2019

Hi km! The best case I can think of is a Matrix, or an array, sort of like a spreadsheet. Where you have could have columns and rows of data. It could look like my_matrix = [['Houston', 'Honolulu', 'Denver', 'Los Angeles'],[95.6, 81.1, 65.2, 73.4], [92.9, 80.4, 67.0, 70.1], [100.6, 79.9, 80.2, 72.8]] where the first row are the city names, followed by temperature data for those cities across 3 more rows. There are few Python packages that specialize in working with this type of data, Numpy and Pandas which uses Numpy.

km on Jan. 2, 2020

Thanks a lot Chris, let me try them out

koellingh on March 27, 2020

I was curious if the min() and max() functions would support both ints and floats in the same list. my initial impulse was that it would not, but it worked. Do floats have ascii values? are ints and floats not compared using ascii values?

Chris Bailey RP Team on March 27, 2020

Hi @koellingh, I think this question is somewhat based on the previous lesson, with min() and max(). Floats and integers can be compared using min and max, because of how Python handles numbers. “Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. A comparison between numbers of different types behaves as though the exact values of those numbers were being compared.” From the python docs about numeric types.

In the case of comparing individual ASCII characters using max() and min(), those functions are using the ordinal/ASCII value.

If you try to compare a mix of characters or strings with numbers using those functions you will get an Error: `TypeError: ‘>’ not supported between instances of ‘int’ and ‘str’.

koellingh on March 27, 2020

Ah, that is very helpful. I understand this now. If ints were treated as ascii values, then there wouldn’t be an error when the min() or max() functions run on a list with both ints and strings. The error is because it is treating ints as ordinals and strings as ascii values, and ordinals and strings cannot be compared together. And ints and floats are both ordinals.

Become a Member to join the conversation.