I am trying to iterate through a list of dictionaries and fetch the values of 2 keys and write the values to another list of dictionaries which has another key. The below is the output list that I am trying to write to.
post_obj = [
{
"city": place["place"],
"display": place["seq"]
}
]
Input:
data = [
{'place': 'San Jose', 'seq': 1},
{'place': 'San Diego', 'seq': 2},
{'place': 'San Fransisco', 'seq': 3}
]
Code:
post_obj = []
if len(data) > 0:
for id in data:
post_obj = [
{
"city": id["place"],
"display": id["seq"]
}
]
else:
post_obj["areas"] = []
Output:
[{'cityName': 'Inline', 'displayOrder': 3}]
Expected Output:
[{'cityName': 'San Jose', 'displayOrder': 1}, {'cityName': 'San Diego', 'displayOrder': 2}, {'cityName': 'San Fransisco', 'displayOrder': 3}]
I don’t know what I’m missing but I’m unable to get the first 2 cities in the list. Any help or input is appreciated. Thank you!