• Register
1 vote
51 views

Problem:

I am trying to adopt the python programming language. Learning it step by step and practicing daily. I’ve written simple code that contains a list and run an iteration on it but it shows: python indexerror: list assignment index out of range

 

m = [1, 2, 3, 5, 8, 13]
n = []
o = 0
for l in m:
    n[o] = l
    o += 1
print n

Where did I wrong? Any Idea?

12 7 7
15,250 points

2 Answers

1 vote

Solution:

Here, n is an empty list, but you're trying to write to an element [0] in the first iteration, which doesn't exist. That’s the reason you are getting the error. However, if you wanted to use the list as an array-like other programming languages you need to define the list as a null value, and then overwrite the values in specific positions.

m = [1, 2, 3, 5, 8, 13]
n = [None] * len(m)
o = 0
for l in m:
   n[o] = l
   n += 1
print n

This should run fine now. Thanks.

13 9 6
94,240 points
0 votes

m is an empty list but you are trying to write the element [0] In the first iteration, which does not exist yet.

Solution:

Try the following instead, to add  a new element to the end of the list;

for l in m:

      n.append(l)

It seems you never do this in practice if you wanted to copy an existing list, then you have to do;

n = list[m]

Python-like array:

Alternatively, if you want to use the python as an array in many other languages, then you have to create a list first whose elements set to be null value and later the values should be in specific positions;

m = [1, 2, 3, 5, 8, 13]

n = [None] * len(i)

#n == [None, None, None, None, None, None]

o = 0



for l in m:

   n[o] = l

   o += 1

Note:

The thing to realize is that a list object will not allow you to assign a value to an index that does not exist.

Another method:

You could also use a list comprehension like;

n = [l for l in m]

Or make a copy of it by using;

n =mi[:]

 

 

11 5 1
3,890 points

Related questions

1 vote
1 answer 15 views
15 views
Problem: I am new in python programming and I have been following a course where I have been instructed to do an assginement to remove all occurrences of a character in a list python. So I write this code: Numbs = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print (" ... main.py", line 8, in <module> if(Numbs[i]==a): IndexError: list index out of range Can anyone help me out to solve this problem?
asked Apr 6 LizzyM 6.1k points
0 votes
1 answer 25 views
25 views
I wrote a code in python : l = [8, 5, 4] total = l[0] + l[1] print (total) def count(a): total = 0 for i in a: total+= a[i] return total print (count(n)) but it has some error: File "main.py", line 11 return total ^ SyntaxError: 'return' outside function What is this error causing for?
asked Mar 15 LizzyM 6.1k points
1 vote
1 answer 35 views
35 views
Problem: Supose I have csv file named &ldquo;liz.csv&rdquo; which contains : Serial,Name,Course,City 1,Raiyan,Python,London 2,Joel,Css,Kyoto 3,Samantha,Python,Paris 4,Porth,Java,Tokyo How can I import this file into lists using python? Thank you
asked Apr 3 LizzyM 6.1k points
1 vote
1 answer 24 views
24 views
Problem: I was discovering String in python. Suddenly I stuck here by getting &ldquo;indexerror: string index out of range&rdquo; this error message. def print_letters(string): count = 0 newstring = [] while count <= len(string): m = string[count] count +=1 print m print_letters("blanks") Do you have any idea? I&rsquo;d appreciate it if you figure the error in my program.
asked Mar 7 Gavin 15.3k points