Is this possible:
myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] = 'c' myList[42] = 'd'
When I try, I get:
# IndexError: list assignment index out of range #
Fill Array With Value With the numpy.fill() function to fill an already existing NumPy array with similar values. The numpy. fill() function takes the value and the data type as input parameters and fills the array with the specified value. We first created the NumPy array array with the np.
Note. python lists are 0-indexed. So the first element is 0, second is 1, so on.
Using the range() function to create a list from 1 to 100 in Python. In Python, we can use the range() function to create an iterator sequence between two endpoints. We can use this function to create a list from 1 to 100 in Python.
You'll have to pre-fill it with something (e.g. 0
or None
) before you can index it:
myList = [None] * 100 # Create list of 100 'None's myList[12] = 'a' # etc.
Alternatively, use a dict instead of a list, as Alex Martelli suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With