Home > Enterprise >  Subtract specific indexed values from lists of list
Subtract specific indexed values from lists of list

Time:09-27

I have a list like

l1 = [[403, 105], [403, 100], [403, 92], [403, 95], [403, 89]].

I want to subtract 403 from all the 403s and check if the difference is 0.

I again want to check the difference of 105 from 100, 92, 95 and 89 respectively and check the difference. The result I want to obtain is:

diffelement1 = [0,0,0,0]
diffelement2 = [5,13,10,16]

Can someone please help me with this?

CodePudding user response:

Try zip unpacking:

diffelement1, diffelement2 = zip(*((abs(403 - x), abs(105 - y)) for x, y in l1[1:]))

>>> diffelement1
(0, 0, 0, 0)
>>> diffelement2
(5, 13, 10, 16)
>>> 

If you want to get the first value dynamically, do:

diffelements = list(zip(*((abs(l1[0][i] - v) for i, v in enumerate(lst)) for lst in l1[1:])))
  • Related