I have a the following data structure:
a = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
b = (('customerB', '1.1.0'), ('customerC', '1.0.1'))
I want the results to be something like this:
[('customerA', None), ('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]
or even skip non existing customers entirely:
[('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]
The zip
function won't help in this case because b
comes from MySQLCursor.fetchall()
with WHERE
clause for the customer name so it won't match with a
if the customer doesn't exist:
>>> [a + (b[1],) for a, b in zip(a, b)]
[('customerA', '1.0.0', '1.1.0'), ('customerB', '1.0.0', '1.0.1')]
>>> import itertools
>>> for a, b in itertools.zip_longest(a, b):
... print(a, b)
...
('customerA', '1.0.0') ('customerB', '1.1.0')
('customerB', '1.0.0') ('customerC', '1.0.1')
('customerC', '1.0.1') None
Have you tried to do it directly?
customers_a = dict(a)
result = [(customer, customers_a[customer], version) for customer, version in b if customer in customers_a]
Now, result
is exactly
>>> result
[('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With