Appending a dictionary to a list in a a loop Python

后端 未结 5 1042
梦毁少年i
梦毁少年i 2020-11-29 23:42

I am a basic python programmer so hopefully the answer to my question will be easy. I am trying to take a dictionary and append it to a list. The dictionary then changes val

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 00:28

    Just put dict = {} inside the loop.

    >>> dict = {}
    >>> list = []
    >>> for x in range(0, 100):
           dict[1] = x
           list.append(dict)
           dict = {}
    
    >>> print list
    

提交回复
热议问题