imhans4305
imhans4305

Reputation: 697

Comparing two list of dictionaries to find common values of a particular field

I have a list of dictionary like this

ref = [{"url":"https://example1.com","category":"sports"},
       {"url":"https://example2.com","category":"movie"},
       {"url":"https://example3.com","category":"sports"},
       {"url":"https://example4.com","category":"sports"}]

and another list of dictionary

ref = [{"url":"https://example1.com","org":"sports"},
       {"url":"https://example5.com","org":"movie"},
       {"url":"https://example2.com","org":"sports"},
       {"url":"https://examplex.com","org":"movie"},
       {"url":"https://example3.com","org":"sports"}]

and I want list of dictionary which have common url value

["https://example1.com","https://example2.com", "https://example3.com"]

I am trying like this

[i for i in new["url"] if i in ref["url"]]

Upvotes: 1

Views: 79

Answers (3)

NicoCaldo
NicoCaldo

Reputation: 1577

A possible solution

ref = [{"url":"https://example1.com","category":"sports"},
       {"url":"https://example2.com","category":"movie"},
       {"url":"https://example3.com","category":"sports"},
       {"url":"https://example4.com","category":"sports"}]

ref2 = [{"url":"https://example1.com","org":"sports"},
       {"url":"https://example5.com","org":"movie"},
       {"url":"https://example2.com","org":"sports"},
       {"url":"https://examplex.com","org":"movie"},
       {"url":"https://example3.com","org":"sports"}]

mylist = []
for x in range(0, len(ref)):
  mylist.append(ref[x]['url'])

for x in range(0, len(ref2)):
  mylist.append(ref2[x]['url'])

newlist = set([x for x in mylist if mylist.count(x) > 1])
print(newlist)

Basically it first merges the elements of the dictionary you need into a single list and the keep only the items that occure more than one time with set

Upvotes: 1

jonsca
jonsca

Reputation: 10381

Something like this uses set comprehensions and is fairly concise:

ref1 = [{"url":"https://example1.com","category":"sports"},
       {"url":"https://example2.com","category":"movie"},
       {"url":"https://example3.com","category":"sports"},
       {"url":"https://example4.com","category":"sports"}]


ref2 = [{"url":"https://example1.com","org":"sports"},
       {"url":"https://example5.com","org":"movie"},
       {"url":"https://example2.com","org":"sports"},
       {"url":"https://examplex.com","org":"movie"},
       {"url":"https://example3.com","org":"sports"}]

ref3 = list({d['url'] for d in ref1} & {d['url'] for d in ref2})

print(ref3)

['https://example1.com', 'https://example2.com','https://example3.com']

Sets are not ordered, so the fact that they come out in numerical order is only by virtue of the ordering of your example list, but you could always just sort the final list in whatever order you would like.

Upvotes: 5

Almos
Almos

Reputation: 139

url_list = list()    
for url in ref:
        for url2 in ref2:
            if url['url'] == url2['url']:
                url_list.append(url['url'])

Upvotes: 1

Related Questions