 # 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. DoubleA

Hi Chris! Thanks for such a clear tutorial! I am learning a lot from you guys. Playing with list I have now something to add which is perhaps worth mentioning here. Here’s my code snippet:

``````import copy
my_list = ['a', ['b', 'c', ['d', 'e', 'f'], 'g', 'h'], 'j', 'i', ['m', ['k', 'l'], 'n']]
deep_copy = copy.deepcopy(my_list)
shallow_copy = my_list.copy()
my_list = '_d_'  # I assign a different value to the chosen element of my_list
print(shallow_copy)       # the element  of the shallow_copy list has also been affected!
print(deep_copy)          # the element  of the deep_copy list has not been affected!

# Copying my_list using the list() function and the slice [:] operator crates also a shallow copy of my_list

my_second_list = list(my_list)
my_third_list = my_list[:]
my_list = '_e_'
print(my_second_list)  # the element of my_second_list has also been affected
print(my_third_list)   # the element of my_third_list has also been affected
``````

One can conclude that it should be borne in mind that simple “copying” of a nested list does not propagate to the nested elements of the copy. I think, there’s a parallel with the “in” operator which, as was demonstrated, did not “see” the nested elements and treated them as standalone or separate lists. Copying nested list using tools other than the `.deepcopy()` method creates shallow copies of the original list and every change to the “parent” list will also be reflected in the subsequent copies. Rupesh Kolatwar

Hello Sir, can you please suggest any real-life example where I can play with LIST

to join the conversation.