+ 10

Why same output??

Why the output of i[0] and i[0][0] is same? Same for i[1] and i[1][0].... Explain how is this working ? https://code.sololearn.com/cannFIrsQD29/?ref=app See this code

1st Jul 2019, 7:31 AM
°°MēDh@°°
°°MēDh@°° - avatar
4 Réponses
+ 7
If you tried the below print statement, you can understand that for each i, python create a list so at i[0] it shows the list, but for the i[0][0] the char. I hope you can understand this. print(list) print(list[0]) print(list[0][0])
1st Jul 2019, 8:59 AM
A.B
A.B - avatar
+ 5
what I want to ask is-- when first time for loop works i has value "Happy always" and i[0]=H and also i[0][0]=H How this is possible because What I think is -- i is a string("Happy always") whose index 0 points to H But how i[0][0] is also H ? Please explain how is this working? how i[0][0] is actually being interpreted?
2nd Jul 2019, 4:33 PM
°°MēDh@°°
°°MēDh@°° - avatar
+ 4
Thanks everyone ..for clearing my doubt....
3rd Jul 2019, 8:27 AM
°°MēDh@°°
°°MēDh@°° - avatar
+ 3
Consider the following list: list = [[1, 2, 3], [4, 5, 6], [[7, 8], [9, 0]]] >>> list[2][0][1] 8 Since lists start at 0, "list[2]" indicates the third item in the list--which is also a list of two other lists--the first item from those two lists is selected with "list[2][0]", and the second element inside of that list is finally selected with "list[2][0][1]". As for your example: print(i[0][0]) i[0] is the letter "H" which is the first element, correct? print(i[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]...etc) returns the first element of "Happy always" which is "H". The first element of "H" is "H" The first element of "H" is "H" The first element of "H" is "H" The first element of "H" is "H" ...continue this for as long as you want.
1st Jul 2019, 9:58 PM
Clayton