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 :: jama api python 
Python :: how to create a subset of a dataframe in python 
Python :: python list max value 
Python :: django or flask 
Python :: bitwise operation in python 
Python :: numpy arange number of elements 
Python :: python rounding 
Python :: pandas count distinct values in column 
Python :: multiline comment 
Python :: update python version pycharm 
Python :: lineplot in plt 
Python :: how to sleep() in python 
Python :: abstract class in python 
Python :: thresholding with OpenCV 
Python :: pynput keyboard backspace 
Python :: python code for twitter scraping using tweepy 
Python :: NEW CALENDAR MODULE 
Python :: count true in a dataframe 
Python :: how to check if a list is empty in python 
Python :: pandas previous row 
Python :: receipt parsing 
Python :: code optimization in python 
Python :: how to make window pygame 
Python :: how to get function help in jupyter notebook 
Python :: duck typing in python 
Python :: harihar kaka class 10 questions 
Python :: unocode error pytonn 
Python :: pystache unescaped characters 
Python :: The current Numpy installation fails to pass a sanity check due to a bug in the windows runtime. 
Python :: check if inf pandas dataframe 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =