Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing two lists of tuples and looking for different values

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')]

Repl Link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading