This is an incredibly simple question (I’m new to Python).
I basically want a data structure like a PHP array — i.e., I want to initialise it and then just add values into it.
As far as I can tell, this is not possible with Python, so I’ve got the maximum value I might want to use as an index, but I can’t figure out how to create an empty list of a specified length.
Also, is a list the right data structure to use to model what feels like it should just be an array? I tried to use an array, but it seemed unhappy with storing strings.
Edit: Sorry, I didn’t explain very clearly what I was looking for. When I add items into the list, I do not want to put them in in sequence, but rather I want to insert them into specified slots in the list.
I.e., I want to be able to do this:
list = [] for row in rows: c = list_of_categories.index(row['id']) print c list[c] = row['name']
Depending on how you are going to use the list, it may be that you actually want a dictionary. This will work:
… or more compactly:
PHP arrays are much more like Python dicts than they are like Python lists. For example, they can have strings for keys.
And confusingly, Python has an array module, which is described as ‘efficient arrays of numeric values’, which is definitely not what you want.