Home > Blockchain >  how to merge two lists in python without using plus
how to merge two lists in python without using plus

Time:03-10

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]

CodePudding user response:

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)]

CodePudding user response:

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

CodePudding user response:

Using list comprehension:

a = ['add ', 'subtract ', 'multiply ', 'divide ']
b = [3, 2, 3, 2]

# Here:
# zip(a, b) iterates through a and b in parallel
# for x,y assigns corressponding values from a and b
# f'{x} {y}' combines the values with a separating space.
# [...] is a "list comprehension"
c = [ f'{x} {y}' for x,y in zip(a, b) ]
print(c)

Outputs:

['add  3', 'subtract  2', 'multiply  3', 'divide  2']

CodePudding user response:

You could do it like this:

list1 = ['add ', 'subtract ', 'multiply ', 'divide ']
list2 = [3, 2, 3, 2]
list3 = []
for y,x in enumerate(list1):
    list3.append("%s%d" % (x,list2[y]))

CodePudding user response:

You can try it:

list1 = ['add ', 'subtract ', 'multiply ', 'divide ']
list2 = [3, 2, 3, 2]

result = []
for i,j in zip(list1,list2):
    result.append(str(i) str(j))
        
print(result)

Output:

['add 3', 'subtract 2', 'multiply 3', 'divide 2']

CodePudding user response:

This is a good case for a "List comprehension"! You can essentially make a small for loop in a python 1-liner as below. Note that to concatenate the values you have to declare the int as a str. You could also do this with f-strings, but I think this is a clearer explanation:

l1 = [1,2,3,4]
l2 = ['a','b','c','d']
result = [str(la) lb for la,lb in zip(l1,l2)]

returns

result = ['1a', '2b', '3c', '4d']

Here we're using zip() it index along two lists simultaneously in the list comprehension, and simply concatenating the elements as we go along.

EDIT: To do this without a plus, we can use an f-string as follows:

result = [f"{la} {lb}" for la,lb in zip(l1,l2)]

This returns the same value, but doesn't use a plus operator (and you don't have to declare a type str(), the f-string does that for you.)

CodePudding user response:

You can add each element together, making sure that numbers are converted to strings:

functions_name = ['add ', 'subtract ', 'multiply ', 'divide ']

main_function_count = [3, 2, 3, 2]

list3 = [name str(count) for name,count in zip(functions_name, main_function_count)]
print(list3)

Output as requested

Or, without using to concatenate strings:

list3 = [f'{name}{count}' for name,count in zip(functions_name, main_function_count)]
  • Related