Clarification for the code

Hi,

When trying to compile below piece of code then getting below error:-
IndexError Traceback (most recent call last)
in
1 my_list = [[0,1,2,3] for i in range(2)]
----> 2 print(my_list[2][0])

IndexError: list index out of range

for below code:-

my_list = [[0,1,2,3] for i in range(2)]
print(my_list[2][0])

what should be done here to resolve this compilation error?

Thanks

You have a list:

[0,1,2,3]

… within a list…

my_list[]

… which is going to look like this:

[[0, 1, 2, 3]]

So, my_list[0] = [0, 1, 2, 3]

There is no my_list[2]

What is it that you’re trying to do, or what did you expect to see?

Thanks for the clarification, I was just trying to understand this piece of code and why was it not getting compiled.

Thanks

similarly i have below piece of code:-

my_list = [3,1,-2]
print(my_list[my_list[-1]])

it’s giving output as ‘1’.

But I am unable to understand this that why it’s giving output as ‘1’ ? could you please clarify it?

Thanks

Okay.

my_list[-1] will return -2 and my_list[-2] will return 1, which is what the print() function is displaying: -1 being the last position of the list and -2 being one position to the left of that.

To add…

Maybe you’ll better understand if you run this:

my_list = ['a','b',-2]

print(my_list[-1])

print(my_list[my_list[-1]])
vals = [0,1,2]
vals.insert(0,1)
del vals[1]
print(vals)

why the above code is showing [1,1,2] but not the expected [1,2] despite using del here?

Thanks

Again, a little easier understand with 'a' 'b' 'c' 'd'

vals = ['a','b','c']
print(vals)

vals.insert(0,'d') # insert 'd' at positon zero  
print(vals)

print(vals[1]) # display position 1
del vals[1]    # delete position 1

print(vals)

A list object is indexed from 0 (zero) to the length of said object.

Maybe this will help:

position:    |  0  |  1 |  2  |  3  |  4  |
 data:       |  a  |  b |  c  |  d  |  e  |
- position:  | -5  | -4 | -3  | -2  | -1  |

What I do when code does not do what I expect is print out variables of interest.

In your code add a print(vals) each time you change vals.
Then you can see what each line of your code does.

Are you trying to make a point that I’ve missed, or are you simply reiterating what I’ve already illustrated?

I wanted to stress that adding the print is a good way to learn to debug your own code.

for below example print(v) behaves differently for same list why so and my_list is getting printed differently here in below examples:-

my_list = [1,2,3]
for v in range(len(my_list)):
 print(v)
 
 my_list.insert(1, my_list[v])
print(my_list)


0
1
2
[1, 1, 1, 1, 2, 3]
**********************************************
my_list = [1,2,3]
for v in range(len(my_list)):
 print(v)
 print(my_list[v])
 my_list.insert(1, my_list[v])
print(my_list)


0
1
1
1
2
1
[1, 1, 1, 1, 2, 3]

Thanks

As range(2) is only 2 elements,
you will have 0,1 index to access it.

>>> ['a' for i in range(2)]
['a', 'a']

>>> a = [0,1,2,3]
>>> my_list = [a for i in range(2)]
>>> my_list
[[0, 1, 2, 3], [0, 1, 2, 3]]

my_list have 0 and 1 as its indexes.
It is a 2 x 4 (row,col) matrix.
The last element is my_list[1][3]

but’s it’s output is not matching with my output , i did not understand where did this matrix come here from in my example?

Thanks

The code line for v in range(len(my_list)): is not always the best way to generate an index; in fact, I’ve read that it should never be used.

A better (or a more accepted) way is to use enumerate, like this:

for index, v in enumerate(my_list):
    print("Index: {} | Item: {}".format(index, v))

I’ve thrown a little print formatting, just so it’s a little easier to screen read the output.

range(n) is a function that return an iterable
starting in 0 and ends in n-1
First step len(my_list) returns 3
Then range(3) returns the iterable (0,1,2)


In each iteration you are altering my_list
For brevity, I have edited the quote to show you
an example.
Run the code and see what is happening.
:point_down:

0 1 [1, 2, 3, 4, 5]
1 a [1, 'a', 2, 3, 4, 5]
2 a [1, 'b', 'a', 2, 3, 4, 5]
3 a [1, 'c', 'b', 'a', 2, 3, 4, 5]
4 a [1, 'd', 'c', 'b', 'a', 2, 3, 4, 5]

:thinking: it is weird. I took :point_down:

and rewrote :point_down:

a = [0,1,2,3]
my_list = [a for i in range(2)]

to be more clear. They are equivalents
and returns the same.

it’s better let’s discuss this one by one of these two examples here:-

for first example here:-

in my first example here i am unable to understand why does print(v) returns values like this

0
1
2

but not like this 0 1 2

secondly for below peice of code:-

 my_list.insert(1, my_list[v])
print(my_list)

as per my understanding above code should put my_list[v] at first index ( which i think should be number 2 as per my example here) but still not clear how below output is getting generated here:-
0
1
2
[1, 1, 1, 1, 2, 3]

Thanks

also for below code it always returns 1 not sure why even if i use -1,-2,1,2 for print(my_list[my_list[2]])

my_list = [3,1,-2]

print(my_list[my_list[2]])

Thanks

What I’ve already posted, in this thread, answers this question, so I’m not too sure if it’s simply not clear to you or if you’re ignoring what I’ve posted.

no i did not understand it Okay.

my_list[-1] will return -2 and my_list[-2] will return 1, which is what the print() function is displaying: -1 being the last position of the list and -2 being one position to the left of that.

To add…

Maybe you’ll better understand if you run this:

my_list = ['a','b',-2]

print(my_list[-1])

print(my_list[my_list[-1]])