Samuel Shifterovich - 1 month ago 16
Python Question

# Combine unknown amount of lists, keep only common values

I want to combine N lists and keep only the values that are in every single one of them. I don't know how many lists there are, so the code must be dynamic.

``````a_list = [(3, -1), (3, -1), (3, 0), (4, -1), (3, 1), (5, -1), (3, 2), (6, -1), (3, 3), (7, -1), (7, -1), (3, 3), (7, 0), (4, 3), (7, 1), (5, 3), (7, 2), (6, 3), (7, 3), (7, 3)]
b_list = [(-3, 3), (-3, 3), (-3, 4), (-2, 3), (-3, 5), (-1, 3), (-3, 6), (0, 3), (-3, 7), (1, 3), (-3, 8), (2, 3), (-3, 9), (3, 3), (3, 3), (-3, 9), (3, 4), (-2, 9), (3, 5), (-1, 9), (3, 6), (0, 9), (3, 7), (1, 9), (3, 8), (2, 9), (3, 9), (3, 9)]
a = set(a_list)
b = set(b_list)
print(list(a&b))
``````

This code works perfectly for a known number of lists, but I don't know how many lists there are.

Note: "Unknown number of lists" means it depends on the values the script is ran with.

Edit: N > 0

You can use the built-in `set` function `intersection`:
``````print (set.intersection(*map(set, lists)))