pt=[2]
pt[0]=raw_input()
when i do this , and give an input suppose 1011 , it says list indexing error- ” list assignment index out of range” . may i know why? i think i am not able to assign a list properly . how to assign an array of 2 elements in python then?
Try this:
You now have two elements in your list. Once you are more familiar with python syntax, you might write this as:
Also, note that lists are not to be confused with arrays in Java or C: Lists grow dynamically. You don’t have to declare the size when you create a new list.
BTW: I tried out your example in the interactive shell. It works, but probably not as you expected:
I’m guessing you thought
pt = [2]would create a list of length 2, so apt[1] = raw_input()would fail like you mentioned:Actually,
pt = [2]creates a list with one element, having the value2at index0:So you can assign to the index 0 as demonstrated above, but assigning to index 1 will not work – use
appendfor appending to a list.