In Python, the TypeError: can't multiply sequence by non-int of type 'list'
error occurs when you try to multiply a sequence type (for example, string, list, tuple, etc.) with a list. This error can be frustrating, especially if you are new to Python programming. In this tutorial, we’ll dive deep into the common scenarios in which this error can error and how to fix it.
Understanding the TypeError: can't multiply sequence by non-int of type 'list'
error
In Python, a sequence is a collection of ordered and indexed elements. The most common sequence types in Python are lists, tuples, and strings. Python allows you to multiply sequences by integer values to create a new sequence that repeats the original sequence.
Let’s look at an example.
# create a list ls = [1, 2, 3] print(ls * 3)
Output:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
In the above example, we multiply the list ls
with the integer 3. You can see that the resulting value is the list elements are repeated 3 times.
You can similarly multiply other sequence types such as strings and tuples with integer values.
# create a string s = "hello" print(s * 3)
Output:
hellohellohello
# create a tuple tup = ('cat', 'dog', 'bunny') print(tup * 2)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
('cat', 'dog', 'bunny', 'cat', 'dog', 'bunny')
Reproducing the error
In the above examples, we saw that we can multiply sequences with integer values to get repeated sequences. But, if you multiply a sequence with a list (for example, you try to multiply a list with another list), you’ll get an error.
Let’s look at an example.
ls1 = [1, 2, 3] ls2 = [0, 1, 0] result = ls1 * ls2 print(result)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 4 1 ls1 = [1, 2, 3] 2 ls2 = [0, 1, 0] ----> 4 result = ls1 * ls2 5 print(result) TypeError: can't multiply sequence by non-int of type 'list'
In the above example, we created two lists of length 3 and then tried to multiply them together using the *
operator. You can see that we get the TypeError: can't multiply sequence by non-int of type 'list'
error.
You’ll also get this error if you try to multiply other sequence types in Python with a list.
# create a tuple tup = (1, 2, 3) # create a list ls = [1, 1, 1] # multiply tuple with the list res = tup * ls print(res)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[5], line 6 4 ls = [1, 1, 1] 5 # multiply tuple with the list ----> 6 res = tup * ls 7 print(res) TypeError: can't multiply sequence by non-int of type 'list'
Why does this error occur?
In the above examples, we saw that multiplying a sequence type in Python with a list value will result in a TypeError: can't multiply sequence by non-int of type 'str'
. This is because you can only multiply a sequence type with an integer and not any other type.
A common scenario in which this error occurs is when you’re trying to multiply two lists containing numeric values together.
# speed in KM/hr speed_ls = [40, 55, 30, 20, 60] # time in hrs time_ls = [4, 2, 3, 1, 1] # distance convered in KM distance_ls = speed_ls * time_ls print(distance_ls)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 6 4 time_ls = [4, 2, 3, 1, 1] 5 # distance convered in KM ----> 6 distance_ls = speed_ls * time_ls 7 print(distance_ls) TypeError: can't multiply sequence by non-int of type 'list'
In the above example, we are given the list of speeds (in KM/hr) for five cars and the respective time taken by them in their journey. We then try to multiply the two lists together to get the distance traveled by each car.
Notice that we get the TypeError
here because we’re trying to multiply a list by another list (you can only multiply a sequence type by an integer).
Fixing the error
To fix the above error, you can use a list comprehension to multiply list values from two (or more) lists together.
A list comprehension is a concise way to create a new list in Python. It allows you to define a new list by filtering and transforming elements from an existing list or other iterable objects. The basic syntax for a list comprehension is:
new_list = [expression for item in iterable if condition]
Here, expression
is the operation to be performed on each item in the iterable, item
is the variable representing each element in the iterable, and condition
is an optional filter that determines whether the item should be included in the new list.
For example, let’s multiply the above two lists together to get a resulting list containing the product of the respective elements using a list comprehension.
# speed in KM/hr speed_ls = [40, 55, 30, 20, 60] # time in hrs time_ls = [4, 2, 3, 1, 1] # distance convered in KM distance_ls = [speed_ls[i]*time_ls[i] for i in range(len(speed_ls))] print(distance_ls)
Output:
[160, 110, 90, 20, 60]
Here, we use a list comprehension to iterate over the indices in the list speed_ls
and then multiply the respective values in the list speed_ls
and the list list time_ls
together. This results in a list containing the respective distance covered by each car. Notice that we don’t get an error here because we’re not multiplying a list with another list.
Alternatively, if you want to perform element-wise multiplication of two lists, you can convert them to numpy arrays and then easily perform the element-wise multiplication using the *
operator.
Conclusion
The TypeError: can't multiply sequence by non-int of type 'list'
error occurs when you try to multiply a sequence type in Python (for example, list, tuple, string) with a list. You can only multiply sequences with integer values in Python which will result in repeated sequences. A very common scenario for this error is multiplying two lists together. To fix this error, you can use a list comprehension to multiply two lists together.
You might also be interested in –