I have a list of numbers, say
data = [45,34,33,20,16,13,12,3]
I'd like to compute the difference between 2 and 2 items, (that is, for the above data I want to compute 45-34,33-20,16-13 and 12-3, what's the python way of doing that ?
Also, more generally, how should I apply a function to 2 and 2 of these elements, that is, I want to call myfunc(data[0],data[1]),myfunc(data[2],data[3])
and so on over the list.
The best way to apply a function to each element of a list is to use the Python built-in map() function that takes a function and one or more iterables as arguments. It then applies the function to each element of the iterables.
Check if list contains duplicates using list.If count > 1 then it means this element has duplicate entries.
Try slicing the list:
from itertools import izip
[myfunc(a, b) for a, b in izip(data[::2], data[1::2])]
or you can use the fact that izip guarantees the order in which it consumes its arguments:
idata = iter(data)
[myfunc(a, b) for a, b in izip(idata, idata)]
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