Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python IndexError: list assignment index out of range

# Why does this iterative list-growing code give IndexError: list assignment index out of range?
# Please consider the following code:
i = [1, 2, 3, 5, 8, 13]
j = []
k = 0

for l in i:
    j[k] = l
    k += 1

#Results in 
IndexError: list assignment index out of range
  
# You are trying to assign to j[0], which does not exist yet 
# Instead, use append:
for l in i:
    j.append(l)
    
# OR initialise j as:
j = [None] * len(i)
Comment

IndexError: list assignment index out of range

for l in i:
    j.append(l)
Comment

PREVIOUS NEXT
Code Example
Python :: python fiboncci 
Python :: python module path 
Python :: python 
Python :: black code formatter 
Python :: datetime print the current time 
Python :: how to write if statement in one line python 
Python :: python only decimal part 
Python :: print to file python 
Python :: snapchat api in python 
Python :: Python __mul__ magic method 
Python :: is python a scripting language 
Python :: how to set python path in mac 
Python :: catch exception python unittest 
Python :: SUMOFPROD1 Solution 
Python :: python not in list 
Python :: chr() function in python 
Python :: pyqt5 line edit font size 
Python :: fill zeros left python 
Python :: pandas filter column greater than 
Python :: re.search() python 
Python :: Max fonction code in python 
Python :: python template strings 
Python :: list methods in python 
Python :: jsonresponse django 
Python :: create 2d array with rows and columns 
Python :: python how to replace a string in a list 
Python :: python api request 
Python :: list to text python 
Python :: run python script without .py 
Python :: normalize function 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =