Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a tuple with map, with conditional evenorodd list

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

like image 313
bryan tetris Avatar asked Jan 02 '23 09:01

bryan tetris


1 Answers

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']
like image 152
Ajax1234 Avatar answered Jan 18 '23 08:01

Ajax1234