RaviKiran
RaviKiran

Reputation: 803

How to update a list that contains values as dictionaries?

Hi frens I have the following form of data

fields = [{'name':'xxx', 'age':24, 'location':'city_name'},
          {'name':'yyy', 'age':24, 'location':'city_name'}]

Now I want to update the location in two dicts and the save the fields in the same format.How to do it?I am beginner.

Upvotes: 0

Views: 37

Answers (1)

falsetru
falsetru

Reputation: 369074

Set same location for both fields.

>>> fields = [{'name':'xxx', 'age':24, 'location':'city_name'},
...           {'name':'yyy', 'age':24, 'location':'city_name'}]
>>> for field in fields:
...     field['location'] = 'loc'
...
>>> fields
[{'age': 24, 'name': 'xxx', 'location': 'loc'}, {'age': 24, 'name': 'yyy', 'location': 'loc'}]

To set different locations, use zip:

>>> for field, loc in zip(fields, ['here', 'there']):
...     field['location'] = loc
...
>>> fields
[{'age': 24, 'name': 'xxx', 'location': 'here'}, {'age': 24, 'name': 'yyy', 'location': 'there'}]

Upvotes: 1

Related Questions