How to remove the same pair with different order in values inside dictionary in Python?

Question:

I have a list of dictionary that look like that

 {'ip_src': '2.2.2.2',
  'ip_dst': '1.1.1.1',
  'src_id': 43,
  'src_name': 'test1',
  'dst_id': 48,
  'dst_name': 'test2'},

 {'ip_src': '1.1.1.1',
  'ip_dst': '2.2.2.2',
  'src': 48,
  'src_name': 'test2',
  'dst': 43,
  'dst_name': 'test1'},


 {'ip_src': '4.4.4.4',
  'ip_dst': '3.3.3.3',
  'src_id': 41,
  'src_name': 'test1',
  'dst_id': 47,
  'dst_name': 'test2'},

 {'ip_src': '3.3.3.3',
  'ip_dst': '4.4.4.4',
  'src': 47,
  'src_name': 'test2',
  'dst': 41,
  'dst_name': 'test1'},

i want to remove the duplicate data connection because the src and dst maybe diffrent but its on the same cable.

so i want that my list will look like this:

 {'ip_src': '2.2.2.2',
  'ip_dst': '1.1.1.1',
  'src_id': 43,
  'src_name': 'test1',
  'dst_id': 48,
  'dst_name': 'test2'},


 {'ip_src': '4.4.4.4',
  'ip_dst': '3.3.3.3',
  'src_id': 41,
  'src_name': 'test1',
  'dst_id': 47,
  'dst_name': 'test2'},

I try to compare every item in the list and make a reverse but it didn’t work because after I look into it, I realised that the order of the items in the dictionary must be 100% reverse and not only the value.

Does someone have an idea how to solve my problem?

Asked By: Ndor

||

Answers:

There are many ways to do it, but simplest one is to convert it to dict with (src_ip, dest_ip) as keys, by dict property duplicates will be removed.

l = [{'ip_src': '2.2.2.2',
  'ip_dst': '1.1.1.1',
  'src_id': 43,
  'src_name': 'test1',
  'dst_id': 48,
  'dst_name': 'test2'},

 {'ip_src': '1.1.1.1',
  'ip_dst': '2.2.2.2',
  'src': 48,
  'src_name': 'test2',
  'dst': 43,
  'dst_name': 'test1'},


 {'ip_src': '4.4.4.4',
  'ip_dst': '3.3.3.3',
  'src_id': 41,
  'src_name': 'test1',
  'dst_id': 47,
  'dst_name': 'test2'},

 {'ip_src': '3.3.3.3',
  'ip_dst': '4.4.4.4',
  'src': 47,
  'src_name': 'test2',
  'dst': 41,
  'dst_name': 'test1'}]

temp_l = {tuple(sorted([i['ip_src'], i['ip_dst']])): i for i in l}
final = list(temp_l.values())

print(final)

# [{'ip_src': '1.1.1.1',
#   'ip_dst': '2.2.2.2',
#   'src': 48,
#   'src_name': 'test2',
#   'dst': 43,
#   'dst_name': 'test1'},
#  {'ip_src': '3.3.3.3',
#   'ip_dst': '4.4.4.4',
#   'src': 47,
#   'src_name': 'test2',
#   'dst': 41,
#   'dst_name': 'test1'}]
Answered By: Epsi95
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.