Update: Sorry for not using carefully break and continue
, whole logic changed just by replacing continue by break, sorry for posting without fundamentals cleared.
I have read this solution but didn't understand.
context: I am working on Odoo framework, and making api call, getting response in list of dictionaries.
response list is very large and I wanted only id, name from that, to add it in database, by making new list of dictionaries.
but when I call update function, it adds values again, so I want to check if that id uniquely exist then skip append, else add that value to list.
api response is like this:
rooms_list = [
{
"id": 1,
"name": "A",
"rates": "dictA"
},
{
"id": 2,
"name": "B",
"rates": "dictB"
},
{
"id": 3,
"name": "C",
"rates": "dictC"
},
]
and my current list is having few values initially as:
new_list = [
{
"id": 1,
"name": "A"
},
{
"id":2,
"name":"B"
}
]
now, it should add only last dictionary to list but it adds redundant values, where am I making mistake?
my whole code is as follows:
print("new_list initially is => ", new_list)
one_record = {}
for i in rooms_list:
for j in new_list:
one_record = {}
print("j is ", j)
if j["id"] == i["id"]:
print("escape this record because it exist already in our new_list")
continue
else:
print("else add it in our new_list")
one_record["id"] = i["id"],
one_record["name"] = i["name"],
if one_record != {}:
print("one_record is not empty for j =>", j)
new_list.append(one_record)
print("rooms_list is ", rooms_list)
print("new_list is ", new_list)
and output is as:
('rooms_list is ', [{'rates': 'dictA', 'id': 1, 'name': 'A'}, {'rates': 'dictB', 'id': 2, 'name': 'B'}, {'rates': 'dictC', 'id': 3, 'name': 'C'}])
('new_list is ', [{'id': 1, 'name': 'A'}, {'id': 2, 'name': 'B'}, {'id': (1,), 'name': ('A',)}, {'id': (2,), 'name': ('B',)}, {'id': (3,), 'name': ('C',)}])
how can I add new dictionaries without redundant id
?