-1

When I am trying to initialize an empty list in Python 3.7.3 using the following code snippet, it is throwing an index error. Can anyone please explain the cause and recommend some corrective action?

newlist = list()
x = 2
for i in range(0, 5):
    newlist[i] = x*2
print(newlist)

O/P: IndexError: list assignment index out of range

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
ArijitC91
  • 33
  • 4
  • 3
    May be what you want is `newlist.append(x*2)`. The `newlist` is empty and you **cannot** access empty list which gives the error. This is similar to `newlist = []`, now if you try `newlist[0]` then, it would give error. – niraj Dec 15 '19 at 13:58
  • But why is the IndexError being thrown? – ArijitC91 Dec 15 '19 at 13:59
  • because you are not using a valid index? it must exist for the access to be valid. – Thomas Dec 15 '19 at 14:00

4 Answers4

1

This is not related to python version. The newlist is empty and you cannot access empty list which gives the error. If you look step by step:

newlist = list()

Viewing newlist at this point gives as:

print(newlist)

will give: []

In the for loop i starts from 0 so, first iteration of the loop will cause:

newlist[0] = x*2

which gives IndexError since list is empty you cannot access by index. So what you probably need is .append to add to the newlist as following:

newlist = list()
x = 2
for i in range(0, 5):
    newlist.append(x*2) # <-- Append to newlist
print(newlist)
niraj
  • 17,498
  • 4
  • 33
  • 48
  • "which gives IndexError since list is empty you cannot access by index" : Just to clarify this point please take a look at the below code snippet. Both the lists in this are validly initialized. However, the code returns the same IndexError, when executed. Any thoughts: vars = list(range(1,10)) squares1 = [0]*9 i = 0 while(i < 10): squares1[i] = vars[i]**2 #error: list index out of range; why?? i+=1 print(squares1)' – ArijitC91 Dec 15 '19 at 14:30
  • Did you try printing value of `i`? In this case i goes from `0` to `9` while you have 9 elements in squares1 i.e. your valid index are `0 to 8`. So, it is expected at index of 9. First look into size of your list by `len(squares1)` and then look into range of your index. – niraj Dec 15 '19 at 14:38
0

You can only access elements at indexes, that exist. So you have to append elements first:

newlist = list()
x = 2
for i in range(0, 5):
    newlist.append(x*2)
print(newlist)
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

Your code is trying to "inject" the value of x*2 into a list at 4 index locations, however, your list is already empty.

So, how can you access those indices to "inject" values into which do not exist in the first place?

You can rectify your code by using append() method which will keep on adding the value of x*2 to the end of the list on each iteration, thereby, creating the required indices in the process:

newlist = list()
x = 2
for i in range(0, 5):
    newlist.append(x*2)
print(newlist)

Hope this clarifies.

Bikramjeet Singh
  • 681
  • 1
  • 7
  • 22
0

A pythonic way to do what you want

x=2    
newlist = [x*2 for x in range(0,5)] 
print(newlist)
LhasaDad
  • 1,786
  • 1
  • 12
  • 19