I have two lists of tuples. For example:
The first one I keep:
[
("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")
]
10 tuples, 4 parameters with subparameters.
The second one I get. This list may have other content:
[
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
6 tuples, 2 parameters with subparameters.
In fact, these are sheets with hundreds of entries.
The filling of the resulting list with tuples is constantly changing, but the general principle of constructing tuples is preserved. How to get only those tuples that have changed in the fastest possible way?
>Solution :
You can use set in Python to determine changed values:
first_list = [("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")]
second_list = [
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
changed = list(set(second_list) - set(first_list))
print(changed)
This outputs:
[('base', 'fourth_val', 'value 2', 'param 1', '100000'),
('base', 'first_val', 'value 1', '10000'),
('base', 'first_val', 'value 2', '5555')]