 # Nesting

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
'a'
>>> print(x, x, x)
a g j
>>> x
['bb', ['ccc', 'ddd'], 'ee', 'ff']
>>> x
'bb'
>>> x
['ccc', 'ddd']
>>> x
'ee'
>>> x
'ddd'
>>> x
'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
False
>>> 'ddd' in x
True
`````` km

Hello Sir, what are some good use cases of NESTED LISTS? Chris Bailey RP Team

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

Thanks a lot Chris, let me try them out koellingh

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

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

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.

to join the conversation.