Python dictionaries-How to keep the new value from overwriting the previous value?

前端 未结 4 630
孤独总比滥情好
孤独总比滥情好 2020-12-21 10:02

I want to create a Dictionary called \"First\" (as in First Name) that will store numerous first names which are all stored in the dictionary via a function. The idea is th

4条回答
  •  心在旅途
    2020-12-21 10:29

    Have a look at collections.defaultdict. Then you can do things like:

    from collections import defaultdict
    
    data['Names'] = defaultdict(list) # Or pass set instead of list.
    data['Names']['first'].append("Bob")
    data['Names']['first'].append("Jane")
    data['Names']['last'].extend("T", "Mart")
    

提交回复
热议问题