Matthew
Matthew

Reputation: 865

Comparing two lists and getting the difference in numbers

I'm trying to understand what the most efficient way to take two lists e.g.

[u'25.78', u'13.39', u'11.93', u'14.97', u'14.34', u'21.08', u'13.24', u'13.11', u'', u'19.45', u'17.56', u'20.57', u'28.50', u'24.38', u'31.13', u'17.20', u'18.52', u'6.42', u'17.31']

and

[u'20.77', u'24.08', u'17.66', u'14.63', u'24.40', u'42.14', u'21.93', u'30.37', u'15.20', u'21.94', u'34.20', u'18.47', u'19.05', u'24.31', u'13.55', u'14.44', u'32.53', u'18.00', u'19.95']

And get a new list with the following:

[u'5.01',u'-10.69',u'-5.73'] and so on..

My brain just isn't comprehending how to do this logically. I keep wanting to refer to a for loop but I am trying to for a for loop inside of another for loop and it's messing me up.

Upvotes: 1

Views: 1116

Answers (1)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

The key is it iterate over both lists at the same time. zip will pair items from each list.

Given listA and listB:

[u'%0.2f' % (float(x) - float(y)) for x, y in zip(listA, listB)]

Upvotes: 6

Related Questions