Python - 如何更新列表标记

Rhy*_*Guy 0 python dictionary list

伪代码:

data = []
data.append({'name': 'John', 'age': '37'})
data.append({'name': 'Joe', 'age': '25'})
Run Code Online (Sandbox Code Playgroud)

这里我做了一些计算,我想更新数据[0],以获得:

data[0] = {'name': 'John', 'age': '37', 'country': 'USA'}
Run Code Online (Sandbox Code Playgroud)

如何更新列表的第一个索引?

Yev*_*ych 7

喜欢:

data[0]['country'] = 'USA' # if you want to set value of country only
Run Code Online (Sandbox Code Playgroud)

要么

data[0] = {'name': 'John', 'age': '37', 'country': 'USA'} # if you want to update whole dictionary 
Run Code Online (Sandbox Code Playgroud)