Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexError: list assignment index out of range in python

IndexError: list assignment index out of range in python What am i doing wrong. I am an newbie

actual_ans_dict = []
for data in prsnobj.result:
    actual_ans_dict[data[0]] = data[1]
    print actual_ans_dict
like image 765
Amy Avatar asked Jan 23 '26 11:01

Amy


1 Answers

actual_ans_dict is an empty list. You are trying to set a value to actual_ans_dict[data[0]] but an element with this index doesn't exist.
You can change the type of actual_ans_dict to dict:

actual_ans_dict = {}
for data in prsnobj.result:
    actual_ans_dict[data[0]] = data[1]
    print actual_ans_dict
like image 64
Deck Avatar answered Jan 25 '26 00:01

Deck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!