I'd like to find the matching values in List 1 and List 2. - Python

Asked 1 years ago, Updated 1 years ago, 123 views

I'm a beginner at Python. I couldn't find what I wanted even if I searched it. I ask for your help me.

List1 = [{'id':1}, {'id':2}, {'id':3 }, {'id':4}]

List2 = [{'id':1}, {'id':4}]

If you compare the value of list1 with the value of list2 in order and get the same value, You want to organize a specific function to run.

python list compare

2022-09-22 16:43

1 Answers

The dictionaries in each list have the same key. This means that you only need to use the value.

>>> List1 = [{'id':1}, {'id':2}, {'id':3 }, {'id':4}]
>>> List2 = [{'id':1}, {'id':4}]
>>> intersection_items = set([item['id'] for item in List1]) & set([item['id'] for item in List2]) # Find the intersection (equal value) in the dictionary of the two lists. 
>>> list(map(lambdai:i*2, intersection_items)) # Apply a function that multiplies the items of the intersection by 2. 


2022-09-22 16:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.