Make List of Unique Objects in Python

后端 未结 1 1946
一整个雨季
一整个雨季 2021-01-18 04:12

It is possible to \'fill\' an array in Python like so:

> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I wanted to use this sample principle t

1条回答
  •  粉色の甜心
    2021-01-18 04:53

    A simple list comprehension should do the trick:

    >>> a = [{'key': 'value'} for _ in range(3)]
    >>> a
    [{'key': 'value'}, {'key': 'value'}, {'key': 'value'}]
    >>> a[0]['key'] = 'poop'
    >>> a
    [{'key': 'poop'}, {'key': 'value'}, {'key': 'value'}]
    

    0 讨论(0)
提交回复
热议问题