I am given a list l
and I want to do assignment:
l[index] = val
But there might be a case when the list is too small.
So, I want to ensure I have space for the new value.
Sometimes I need to fill the new space with empty strings ''
, and sometimes with other objects (like empty lists []
, False
or None
).
For this task I use the following procedure:
def ResizeList(l, size, fill_with=None):
l += [fill_with]*(size-len(l))
(note: it works even if size-len(l)<=0
)
(note: as I am interested in reserving space, I intentionally DO NOT truncate it to a shorter list)
Like that:
ResizeList(l, index+1)
l[index] = val
(When filling with other object, it's like : ResizeList(l, index+1, [])
)
Are there more pythonic ways of doing that? Are there some built-ins or library functions for doing this?
I am using mostly Python-3.x, but know-how about Python-2x is useful and welcome.
Clarification: Please, do not tell me about dict
, cause I need list
For those who would like me to be more specific:
The problem statement states it's about list
type. Using dict
here is not an option or solution. There are reasons for that, specifically related to the domain (I am doing a prototype of an experiment that has to show some asymptotic behaviour, not - as probably you're used to - a prototype of a program. If it would be "just a prototype of a program", then I agree with using a dict and the other comments). I have the following assumptions:
- I have many many lists (need to care about memory and performance overhead)
- due to workflow and need of prototype, I cannot call a handcoded C/C++ extension
- during computation the final list size is unknown
- we know that in the and the lists will be dense
- list cells are written and overwritten in an unknown order
Those are just a few reasons why I have stressed that I need a list
and not a dict
.
For those interested in more details or who would like to discuss about dict
, checkout how we discuss in comments HERE