What is the most pythonic way to reverse the elements of one list based on another equally-sized list?
lista = [1,2,4]
listb = ['yes', 'no', 'yep']
# expecting [-1, 2,-4]
[[-x if y in ['yes','yep'] else x for x in lista] for y in listb]
# yields [[-1, -2, -4], [1, 2, 4], [-1, -2, -4]]
if listb[i] is yes or yep, result[i] should be the opposite of lista.
Maybe a lambda function applied in list comprehension?
using zip?
>>> [-a if b in ('yes','yep') else a for a,b in zip(lista, listb)]
[-1, 2, -4]
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