Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going through multidimensional lists

I have who lists:

list_a = [['abc', 10],['def', 20],['ghj', 30]]
list_b = [['abc', 40],['def', 50],['ghj', 60],['klm', 70]]

and I want to go through list.b with list.a so I can remove the spare element "list.b[3]" and calculate with list members:

list_a[0][1] - list_b[0][1] = -30
list_a[1][1] - list_b[1][1] = -30
list_a[2][1] - list_b[2][1] = -30

What on earth I should do?

like image 603
noobiedoobie Avatar asked Jun 18 '26 01:06

noobiedoobie


1 Answers

Use zip. It will automatically trim to the length of the shortest iterable. Here it is in a list comprehension:

[a_val[1] - b_val[1] for a_val, b_val in zip(a,b)]
like image 76
Patrick Haugh Avatar answered Jun 20 '26 15:06

Patrick Haugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!