Reputation: 398
I learned C over last summer, using K&R 2nd ed. book on C from 1989. I decided then to take CS50, and am now learning python3 for the first time.
I also decided to take a tutorial on python3, online at http://www.python-course.eu/python3_sequential_data_types.php,. I'm having some trouble understanding pythons indexing for deeply nested lists.
I've spent a while searching before posting, but did not see the answer. The examples i found online are of this kind:
>>> t = [[100, 'str_1'], [200, 'str_2'], [300, 'str_3']]
which i do understand. The indexing is the same as C 2d char array.
What is confusing me is this:
place= ["High up", ["further down", ["and down", ["deep down", "the answer", 42]]]]
>>> place[0]
'High up'
>>> place[1]
['further down', ['and down', ['deep down', 'the answer', 42]]]
>>> place[1][1]
['and down', ['deep down', 'the answer', 42]]
>>> place[1][1][1]
['deep down', 'the answer', 42]
>>> place[1][1][1][0]
'deep down'
>>> place[1][1][1][0][3]
'p'
I thought i got it, just keep going over one, to get to the next list, but then i found this.
>>> complex_list = [["a",["b",["c","x"]]],42]
complex_list[0] #note: index 0 is the entire left, while above it's not.*
['a', ['b', ['c', 'x']]]
>>> complex_list[0][1]
['b', ['c', 'x']]
>>> complex_list[0][1][1][0]
'c'
The two list look almost the same to me, except that complex_list has two braces on the left. Can someone explain the rule to me, I don't see why place[0] is only the first item in the list, while complex_list[0] is the entire list except for the number 42? How does that extra brace change the indexing?
Upvotes: 0
Views: 4915
Reputation: 107
I hope this helps you. The two braces at the beginning of complex_list imply that what ever the length of complex_list it's first item complex_list[0] is another list, where as in place your first item place[0] is just a string 'High up'.
Now, you should know a string is a set of characters which could also be accessed using indexing, so you could also access different elements in item place[0] as follows:
print(place[0][0]) # first element in string of characters 'High up'
# H
print(place[0][1]) # second element in string of characters 'High up'
# i
print(place[0][5]) # sixth element in string of characters 'High up'
# u
All the elements printed here are characters in the string found in item one of the list place.
In the case of complex_list, the first item complex_list[0] is a list and so can further be accessed by indexing while the second item complex_list[1] is an integer which cannot be indexed further
Upvotes: 0
Reputation: 27869
Hope this helps you understand nesting a bit better:
a = ['this element is in outer list', #index No. [0]
['this is inner list', #index No. [1] to get whole list and index No. [1][0] to get 1st element
['this is inner list within inner list', #index No. [1][1] will get you list and No. [1][1][0] will get 1st element
['this is inner list within inner list that is within inner list']]], #index No. [1][1][1]
'this element is in outer list', #index No. [2]
['this is inner list'], #index No. [3] if you want that list and index No. [3][0] to get string
'this element is in outer list'] #index No. [4]
print a[0] #this element is in outer list
print a[1] #['this is inner list', ['this is inner list within inner list', ['this is inner list within inner list that is within inner list']]]
print a[1][0] #this is inner list
print a[1][1] #['this is inner list within inner list', ['this is inner list within inner list that is within inner list']]
print a[1][1][0] #this is inner list within inner list
print a[1][1][1] #['this is inner list within inner list that is within inner list']
print a[2] #this element is in outer list
print a[3] #['this is inner list']
print a[3][0] #this is inner list
print a[4] #this element is in outer list
Furthermore, you can access string
s as if they are list
s therefore if you have:
b = 'Example'
print b[0] #this will print E
print b[1] #this will print x
Upvotes: 0
Reputation: 3973
You can think about it as each list within a list being a separate item. Whatever is in the list is irrelevant until you access it.
For your example:
place = ["high up", [...]]
place[0]
is "high up"
because the first item in place
is that string.
complex_list = [[...], 42]
complex_list[0]
is the whole list except 42 because the list is the first item.
Upvotes: 1