I want to assign values to different indices and not in a sequential manner(by using append), like we would do in a hash table. How to initialize the array of a given size? What I do:
a=[]
for x in range(0,10000):
a.append(0)
Is there a better way? Also, is there any function like memset() in c++?
As noted in the comments, initializing a list
(not an array, that's not the type in Python, though there is an array
module for more specialized use) to a bunch of zeroes is just:
a = [0] * 10000
If you want an equivalent to memset
for this purpose, say, you want to zero the first 1000 elements of an existing list
, you'd use slice assignment:
a[:1000] = [0] * 1000
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