0
a = int(input())
l1 = []
for i in range(a):
    l1[i] = 5

print(l1)

I keep getting the error:

list assignment index out of range

i will always be smaller than a so why am I getting this error? I don't wish to use append().

17
  • 7
    "I don't wish to use append()" Why? Commented Sep 3, 2018 at 15:21
  • The error occurs because l1 has length zero, so you cant index into l[i] for any i. Why you don't want to use append is beyond me. Commented Sep 3, 2018 at 15:23
  • i have solved the issue using append() but wish to learn how to populate lists in python without using append() Commented Sep 3, 2018 at 15:24
  • 2
    append() is the one obvious way to do it. Using anything else would make for worse code. Commented Sep 3, 2018 at 15:25
  • 1
    Just try list comprehension as l1 = [5 for _ in range(10)] and print (l1) if you are so against the append(). Commented Sep 3, 2018 at 15:25

5 Answers 5

1

You could use this if you want to get a list of n '5' elements:

a = int(input())
l1 = [5 for _ in range(a)]
print l1

It will wait for input and save the list in the l1 list

Sign up to request clarification or add additional context in comments.

1 Comment

I would replace i by _ since it's not being used here
0

You are referring to the i-th element, but your list is empty. It has to have a i-th element to write a[i] = 5. So, populate your list with a dummy value. eg, start with l1 = [None] * a instead of l1 = [].

Comments

0

Use insert.

a = int(input())
l1 = []
for i in range(a):
    l1.insert(999999999999,5)

print(l1)

Executing:

 10
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

Comments

0

You might be confusing the behavior of list to dictionary. Dictionary would work.

a = int(input())
l1 = {}
for i in range(a):
    l1[i] = 5  

print([j for i,j in l1.items()])

You got an error because there exists no element at l1[0] to be assigned users input. If we were to make your code work, we need to pre-populate the list with dummies.

a = int(input())
l1 = (','*(a-1)).split(',')
for i in range(a):
    l1[i] = 5

print(l1)

5 Comments

I don't think he is confused between Dictionary and List. He is treating List like Arrays, maybe he just started python and trying to apply other Language paradigms in Python
Ah! Would not array throw IndexError?
python doesn't have arrays. If arrays were to initialize then no they won't throw error if you do array[0]=someValue
I was think of numpy: I.e. l1 = np.array([]). This throws IndexError ;)
@VishalSingh Python does have arrays.
0

It seems to me that the simplest change is to append the elements, rather than use an index. The list doesn't need to be pre-sized and the code is easy to read for someone coming from another language. Replace "i" with "_" if you like.

a = int(input())
l1 = []
for i in range(a):
    l1.append(5)

print(l1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.