I have two list in same size in python and want to merge them to become one list with the same number size as before
first one :
['add ', 'subtract ', 'multiply ', 'divide ']
second one :
[3, 2, 3, 2]
and i want output came like :
['add 3', 'subtract 2', 'multiply 3', 'divide 2']
How can I do that?
I tried this:
list3 = functions_name + main_function_count
but the output is :
['add ', 'subtract ', 'multiply ', 'divide ', 3, 2, 3, 2]
>Solution :
+ is adding the lists themselves, you want to apply + to each element.
ops = ['add ', 'subtract ', 'multiply ', 'divide ']
nums = [3, 2, 3, 2]
list3 = [op+str(n) for op, n in zip(ops, nums)]
# or using an fstring to remove "+" entirely
list3 = [f"{op}{n}" for op, n in zip(ops, nums)]
zip lets you iterate multiple "iterables", like lists, in parallel.
edit: changed n to str(n), fstring