Can you try this below code and I think you expected answer shouldn't have same key names so I have renamed it to student_name
as key(if required you can change name
to room_name
). I hope that should be fine for you as well.
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
relocation_list = []
for student_dict in student_list:
for room_dict in room_list:
new_dict = {}
if student_dict['room'] == room_dict['id']:
new_dict["student_name"] = student_dict['name']
new_dict.update(room_dict)
relocation_list.append(new_dict)
with open('relocation.json', 'w') as f:
f.write(str(relocation_list))
print(relocation_list)
output:
[{'student_name': 'Ryan Keller', 'id': 2, 'name': 'Room #2'}, {'student_name': 'Brooke Ferrell', 'id': 1, 'name': 'Room #1'}, {'student_name': 'Travis Tran', 'id': 0, 'name': 'Room #0'}]
Using list comprehensive way:
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
print([{**room_dict, "student_name": student_dict['name']} for room_dict in room_list for student_dict in student_list if student_dict['room'] == room_dict['id']])
# output
# [{'id': 0, 'name': 'Room #0', 'student_name': 'Travis Tran'}, {'id': 1, 'name': 'Room #1', 'student_name': 'Brooke Ferrell'}, {'id': 2, 'name': 'Room #2', 'student_name': 'Ryan Keller'}]
EDIT
For new requirement mentioned in comments
- Avoid duplicate Ids in
relocation_list
- Here we merge values of student_name
to list of student
- Sort list of dict by value - Ref: Check this link there are many other ways too
import json
with open('files/rooms.json') as f:
room_list = json.load(f)
with open('files/students.json') as file:
student_list = json.load(file)
relocation_list = []
# We arent considering about duplicate item as it is created by same loop
def check_room_id_in_relocation_list(id_to_check):
is_exist, index, item = False, None, {}
for index, item in enumerate(relocation_list):
if id_to_check == item['id']:
is_exist, index, item = True, index, item
return is_exist, index, item
for student_dict in student_list:
for room_dict in room_list:
new_dict = {}
if student_dict['room'] == room_dict['id']:
is_room_exist, index, item = check_room_id_in_relocation_list(room_dict['id'])
if is_room_exist:
# To merge same ids
# since it already exist so update list's item with new student_name
# instead of new key we are assigning list of names to key "student_name" to
relocation_list[index]['student_name'] = [relocation_list[index]['student_name'], 'new nameeeee']
else:
new_dict["student_name"] = student_dict['name']
new_dict.update(room_dict)
relocation_list.append(new_dict)
print("student list:", student_list)
print("room list: ", room_list)
print("relocation_list:", sorted(relocation_list, key=lambda k: k['id']))
with open('relocation.json', 'w') as f:
f.write(str(sorted(relocation_list, key=lambda k: k['id'])))
# output
# student list: [{'id': 0, 'name': 'Ryan Keller', 'room': 2}, {'id': 1, 'name': 'Brooke Ferrell', 'room': 1}, {'id': 2, 'name': 'Travis Tran', 'room': 0}, {'id': 3, 'name': 'New User', 'room': 2}]
# room list: [{'id': 0, 'name': 'Room #0'}, {'id': 1, 'name': 'Room #1'}, {'id': 2, 'name': 'Room #2'}]
# relocation_list: [{'student_name': ['Travis Tran', 'new nameeeee'], 'id': 0, 'name': 'Room #0'}, {'student_name': 'Brooke Ferrell', 'id': 1, 'name': 'Room #1'}, {'student_name': 'Ryan Keller', 'id': 2, 'name': 'Room #2'}]