Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge two lists in python without using plus

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]
like image 578
Talia Dianal Avatar asked Mar 03 '26 18:03

Talia Dianal


1 Answers

Use a combination of list comprehension with zip and f-strings

list1 = ['add ', 'subtract ', 'multiply ', 'divide ']
list2 = [3, 2, 3, 2]
result = [f'{x} {y}' for x, y in zip(list1, list2)]
like image 68
dgw Avatar answered Mar 05 '26 08:03

dgw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!