Question: This is my code:  from collections import defaultdict ip_port_device = [ {'ip': '192.168.1.140', 'port_number': 4, 'device_name': 'device1'}, {'ip': '192.168.1.128', 'port_number': 8, 'device_name': 'device1'}, {'ip': '192.168.1.56', 'port_number': 14, 'device_name': 'device1'}, {'ip': '192.168.1.61', 'port_number': 4, 'device_name': 'device1'}, {'ip': '192.168.1.78', 'port_number': 8, 'device_name': 'device1'}, {'ip': '192.168.1.13', 'port_number': 16, 'device_name': 'device1'}, {'ip': '192.168.2.140', 'port_number': 4, 'device_name': 'device2'}, {'ip': '192.168.2.128', 'port_number': 8, 'device_name': 'device2'}, {'ip': '192.168.2.56', 'port_number': 14, 'device_name': 'device2'}, {'ip': '192.168.2.61', 'port_number': 4, 'device_name': 'device2'}, {'ip': '192.168.2.78', 'port_number': 8, 'device_name': 'device2'}, {'ip': '192.168.2.13', 'port_number': 16, 'device_name': 'device2'}, {'ip': '192.168.3.140', 'port_number': 4, 'device_name': 'device3'}, {'ip': '192.168.3.128', 'port_number': 8, 'device_name': 'device3'}, {'ip': '192.168.3.56', 'port_number': 14, 'device_name': 'device3'}, {'ip': '192.168.3.61', 'port_number': 4, 'device_name': 'device3'}, {'ip': '192.168.3.78', 'port_number': 8, 'device_name': 'device3'}, {'ip': '192.168.3.13', 'port_number': 16, 'device_name': 'device3'}, ] ip_per_node = [ {'node_name': 'server9.example.com', 'ip_address': '192.168.1.140'}, {'node_name': 'server19.example.com', 'ip_address': '192.168.1.128'}, {'node_name': 'server11.example.com', 'ip_address': '192.168.2.140'}, {'node_name': 'server21.example.com', 'ip_address': '192.168.2.128'}, {'node_name': 'server17.example.com', 'ip_address': '192.168.3.140'}, {'node_name': 'server6.example.com', 'ip_address': '192.168.3.128'}, ] ips_and_ports_in_switch = [] for compute in ip_per_node: for port in ip_port_device: if compute['ip_address'] == port['ip']: port = port['port_number'] for new_port in ip_port_device: if port == new_port['port_number']: ips_and_ports_in_switch.append({ 'port_number': new_port['port_number'], 'ip_address': new_port['ip'], 'node_name': compute['node_name'], 'device_name': new_port['device_name'] }) concatenated = defaultdict(list) for entry in ips_and_ports_in_switch: concatenated[(entry['device_name'], entry['port_number'], entry['node_name'])].append(entry['ip_address'])  The logic is: if ip_per_node['ip_address'] matches ip_port_device['ip'], then in ip_port_device find all ips have the same port number. Then save like this (expected output): node server9.example.com, port 4, device device1, ips ['192.168.1.140', '192.168.1.61'] node server19.example.com, port 8, device device1, ips ['192.168.1.128', '192.168.1.78'] node server11.example.com, port 4, device device2, ips ['192.168.2.140', '192.168.2.61'] node server21.example.com, port 8, device device2, ips ['192.168.2.128', '192.168.2.78'] node server17.example.com, port 4, device device3, ips ['192.168.3.140', '192.168.3.61'] node server6.example.com, port 8, device device3, ips ['192.168.3.128', '192.168.3.78']  My current code doesn't work as I expect. It saves one port multiple times for all nodes. I tried to add least but needed data for the sample.

Answer: # loop both lists to match nodes with port_number and device_name for item in ip_port_device: for node in ip_per_node: if item["ip"] == node["ip_address"]: node.update( { "port_number": item["port_number"], "device_name": item["device_name"], "ips": [item["ip"]], } ) # loop both lists again to get ips that have no direct correspondence in ip_per_node for item in ip_port_device: for node in ip_per_node: if ( item["port_number"] == node["port_number"] and item["device_name"] == node["device_name"] and item["ip"] not in node["ips"] ): node["ips"].append(item["ip"]) for node in ip_per_node: print(f'node {node['node_name']}, port {node['port_number']}, device {node['device_name']}, ips {node['ips']}')  node server9.example.com, port 4, device device1, ips ['192.168.1.140', '192.168.1.61'] node server19.example.com, port 8, device device1, ips ['192.168.1.128', '192.168.1.78'] node server11.example.com, port 4, device device2, ips ['192.168.2.140', '192.168.2.61'] node server21.example.com, port 8, device device2, ips ['192.168.2.128', '192.168.2.78'] node server17.example.com, port 4, device device3, ips ['192.168.3.140', '192.168.3.61'] node server6.example.com, port 8, device device3, ips ['192.168.3.128', '192.168.3.78']