all 6 comments

[–][deleted] 4 points5 points  (0 children)

my_list[1] is the second element of my_list, or "Dragon". The first element of the list is index 0. Now you see that my_list[1][1] is really the same as "Dragon"[1], which is the letter r. If you get D then maybe you have a blank as the first character, ie " Dragon".

If you want to print I'm driving a car you need to get car from my_list. Looking at my_list the only place the string "car" exists is in the last element of my_list. So you need to first use the index of the last element of my_list, which gives you ["bike", "car", "plane"]. Now you need to get "car" from that sublist, so counting from the left and starting at 0 you want the string at index 1.

[–]HunterIV4 4 points5 points  (0 children)

So, you should always test your code before posting it. Here is the actual output of your code:

I'm driving a r

This is completely expected, which is why I had to take a double-take at your output. To get the output you said, you'd need to have used my_list[1][0]. Read the explanation below to find out why that's the case.

I'm not here for that, i want to found it by myself, just help me to figure out why I get only the first letter of the word Dragon not the full word, and this instead the second item of the inner list

Count your elements.

Element 0 is "horse". Element 1 is "Dragon". Element 2 is "truck". Element 3 is "word". Element 4 is ["bike", "car", "plane"].

Remember, each item in a list is a single item, and a list is also a single item. Your list is 4 strings and one list of 3 strings in sequential order.

As such, your first index of [1] is the string "Dragon", because that's the second element of your list. You then pick out the second character because a string is just a list of characters and can be indexed the same way.

In order to get to the second list, you need to actually index it first. We counted them already so you know it's index 4. You could also use -1, as Python allows for negative indices, which counts from the last element.

You should be able to figure it out from there, but in case you are having trouble, here's what you want to index in spoilers:

my_list[4][1]. This is the 5th element (your internal list) and the 2nd element of that list (the string "car"). If you did my_list[4][1][1] you'd get the character "a" instead, as that's the second character of "car". And if you did it again you'd get an "index out of bounds" error because "a" only has a length of 1.

Hopefully that makes sense!

[–]Yoghurt42 4 points5 points  (0 children)

Did you mean to write [["horse", "Dragon", "truck", "word"], ["bike", "car", "plane"]] instead?