Reputation: 365
Shouldn't the following code print? 100 100
price = 100 # assigns 'price' reference to 100
price = [price] # creates a 'price' list with 1 element: [100]
for i in range(1, 3):
print(price[0]) # prints 100
price[i] = price[i - 1]
price.append(price[i])
print(price[i])
Getting a IndexError: list assignment index out of range
error at line price[i] = price[i - 1]
, but the line right before prints 100
successfully. Shouldnt price[i] simply be getting assigned price[0] value?
Upvotes: 0
Views: 65
Reputation: 33940
You're trying to append items to a list, or more precisely to initialize a list with repeated copies of something. Here are Pythonic ways to do that:
# Use a list comprehension
>>> price = [100 for _ in range(3)]
[100, 100, 100]
# Use itertools.repeat
>>> import itertools
>>> list(itertools.repeat(100, 3))
[100, 100, 100]
These are both faster than (repeatedly) doing append()
, which is O(N), so repeatedly doing append()
is O(N^2) on a long list, which gets very slow.
(Btw if you know a priori the list will have at least N elements, and N is large, you could initialize it to price = [None] * N
and you get [None, None, None...]
. Now you can directly assign to them. But, explicitly doing append is better practice for beginners.)
Upvotes: 2
Reputation: 1000
That will not print out:
100
100
You initialized a list with 1 element, the size of that list is 1. However your range starts at 1 for the for
loop , what's really happening is:
price = 100 # assigns 'price' to 100
price = [price] # creates a 'price' list with 1 element: [100]
for i in range(1, 3): # The list is 0-indexed, meaning price[0] contains 100
print(price[0]) # prints 100 as it should
price[i] = price[i - 1] # i is 1, price[i] is not an assigned value, i.e: you never assigned price[1]
price.append(price[i]) # This doesn't execute because an exception was thrown
print(price[i]) # Neither does this
To get the result you're looking for, this would work:
price = [100] # creates a 'price' list with 1 element: [100]
for i in range(0, 2): # Start at index 0
print(price[i]) # Print current index
price.append(price[i]) # Append current value of price[i] to the price list
To ensure everything appended as you expected you can test it with len:
print(len(price))
Output:3
However, it is a preferred way of appending as @smci has shown in his/her answer.
Upvotes: 0
Reputation: 6888
You can't assign a value to a list directly why this list has note a previous size defined. You have to use append to add elements to the position you want.
Check:
# Check the value on our initial position
print(price[0])
for i in range(1, 3):
price.append(price[i-1])
print(price[i])
Upvotes: 0
Reputation: 1713
The problem is you are assigning at an index too great for the length of the array.
for i in range(1, 3):
This initializes i to 1. Since arrays are zero-indexed, and the length of your array is 1 on the first pass, you will hit an assignment out of range error
at the line you mentioned (when i=1
).
Here is a minimal example showing the issue:
my_array = ["foo"]
my_array[1] = "bar" # throws assignment out of range error
Upvotes: 0
Reputation: 13106
If you are just trying to append to a list, trying to do that with an index won't exactly work because that index isn't present in the list:
somelist = []
somelist[0] = 1
IndexError
So just use append
for i in range(1,3):
price.append(price[i-1])
Upvotes: 0