Python list set value at index if index does not exist

后端 未结 4 700
感动是毒
感动是毒 2020-12-11 01:47

Is there a way, lib, or something in python that I can set value in list at an index that does not exist? Something like runtime index creation at list:

l =          


        
4条回答
  •  眼角桃花
    2020-12-11 02:20

    Not foolproof, but it seems like the easiest way to do this is to initialize a list much larger than you will need, i.e.

    l = [None for i in some_large_number]
    l[3] = 'foo'
    # [None, None, None, 'foo', None, None None ... ]
    

提交回复
热议问题