Here is my code :
evenorodd=[1,2,3]
list1=['a','b','c']
list2=['A','B','C']
res = tuple(map(lambda x: True if x % 2 != 0 else False, evenorodd))
print(res)
the output:
(False, True, False, True)
I want this : element of list1 if x%2!=0 (if element of evenorodd is odd) element of list2 else (if element of evenorodd is even) The output that I look for:
('a','B','c')
and I want to do that on one line
res = tuple(map(lambda x: ??? if x % 2 != 0 else ???, evenorodd))
Thank you
You can use zip
:
evenorodd=[1,2,3]
list1=['a','b','c']
list2=['A','B','C']
new_result = [a if c%2 == 0 else b for a, b, c in zip(list2, list1, evenorodd)]
Output:
['a', 'B', 'c']
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