How to multiply two string lists in python

Beginner question: If I have 2 lists containing only strings, how do I concatenate them in a third list containing all the elements + each element of one list + each element in the other list? I thought about for loops, but isn't there a more simple way to do it? Example:

listone = ['cat', 'dog' ]
listtwo = ['bin', 'sun'] 

resulting list = ['cat', 'dog', 'bin', 'sun', 'catbin', 'catsun', 'dogbin', 'dogsun', 'catbinsun', 'catsunbin', 'catcat', 'catdog', 'dogbin', 'dogcat']

EDIT: Thank you all for replies, but i didn't explain what I want to do well enough. The number of string in each list should be indefinite, and every word has to be concatenated with every other word, but not only in the form "x + y". I want also to concatenate it to the other words. Like u = x+y+z

In this tutorial, we are going to discuss some methods which are used to multiply two lists in Python. This tutorial has some unique and simple methods to perform the multiplication of both lists. This tutorial will demonstrate various methods to perform the element-wise multiplication of two lists in Python. Suppose we have two lists of integers with the same size and we want to perform the multiplication of elements of the first list with the elements at the same index in the second list and get a resultant list with the same size.

Some of the common ways to multiply two lists are listed below.

  • Multiply two lists using For Loop
  • Multiplying two Lists using List Comprehension
  • Multiplication of two lists using Numpy
  • Multiply Both Lists using the Naive technique

If you haven’t seen our tutorial for List Multiplicationand Product of Lists in Python, also take a look at them for a better understanding.

We can simply get the product of two lists using for loop. Through for loop, we can iterate through the list. Similarly, with every iteration, we can multiply the elements from both lists. For this purpose, we can use Zip Function. The zip() function in python can combine the contents of 2 or more iterables. Zip Function returns a zipped output. We can then simply store the output in Result and Display it on the console. This is a very simple way to perform list multiplication in Python.

#Using for loop

list1 = [1, 2, 3, 4, 5]
list2 = [6, 5, 4, 3, 3]

Result = []
for i1, i2 in zip(list1, list2):
  Result.append(i1*i2)
  

print("The product of 2 lists is: ", Result)
 

Output:

The Product of 2 lists is: [6, 10, 12, 12, 15]

Now another interesting situation arises if list 1 has more elements than list 2. In this case, the zip() function helps to discard the last element and make sure that every element is multiplied by the other element of another list with the same index number.

#Using for loop

list1 = [1, 2, 3, 4, 5, 6]
list2 = [6, 5, 4, 3, 3]

Result = []
for i1, i2 in zip(list1, list2):
  Result.append(i1*i2)
  

print("The product of 2 lists is: ", Result) 
 

Output:

The product of 2 lists is: [6, 10, 12, 12, 15]

Multiply two lists using List comprehension

List comprehension is another method for multiplying both lists in python. Let’s see how it works. This is a very simple code that requires no complex syntax. If you don’t know about List Comprehension, Visit this.

#Using list comprehension

list_1 = [9, 8, 7]
list_2 = [1, 2, 3]

result = [i1 * i2 for i1, i2 in zip(list_1, list_2)]

print(result)
 

Output

[9, 16, 21]

Since we are using Zip() Function here, there is no need to worry about any of the elements in both lists. Zip Function will multiply the elements with each other as long as the last element of the shortest list is reached.

Multiply two lists using numpy

First of all, we will have to import the Numpy library. Secondly, we will find the list which has the minimum number of elements in it. Then we will convert these lists into NumPy. And Lastly, we will multiply the elements of list 1 by the elements of list 2 with the same index numbers.

#Using Numpy

import numpy as x

list1 = [1, 2, 3, 4, 5]
list2 = [9, 8, 7, 6, 5, 6]

n = min(len(list1), len(list2))

array1 = x.array(list1)
array2 = x.array(list2)

result = array1[:n]*array2[:n] 

result = result.tolist()
print("The product of both lists is: ", result)
 

Output

The product of both lists is: [9, 16, 21, 24, 25]

Multiply Both Lists using the Naive technique

Here is another interesting method to multiply both lists. Let’s discuss it. This is again a very simple method. We have used for loop here to multiply the elements of both lists with the same index number.

#Using Naive technique

list1 = [1, 3, 4, 6, 8]
list2 = [4, 5, 6, 2, 10]

result = []
for x in range(0, len(list1)):
    result.append(list1[x] * list2[x])
  
print ("Product of both lists is : " + str(result))'
 

Output

Product of both lists is : [4, 15, 24, 12, 80]

Please leave feedback here. Make sure to see our other tutorials to learn Python and Matlab.

See more Python List Tutorials

How do you multiply a string list in Python?

This has probably been asked before, please read the language's docs at docs.python.org and consider using a simple "for i in range" loop where you can c += [a[i]] * b[i] in each iteration. A better duplicate target: Repeat each item in a list a number of times specified in another list.

Can 2 strings be multiplied in Python?

In Python, the only operators that work with strings are addition and multiplication. You can't use strings if you're subtracting or dividing.

What happens when you multiply a list in Python?

If you multiply a python list by a number, it gets repeated N times. Copied! Multiplying a Python list by N, returns a new list containing the elements of the original list repeated N times.

How do you multiply all items in a list in Python?

Use the syntax [element * number for element in list] to multiply each element in list by number ..
a_list = [1, 2, 3].
multiplied_list = [element * 2 for element in a_list].
print(multiplied_list).