TypeError: can't multiply sequence by non-int of type 'list' [Fixed]
Written by Kolade Chris | Nov 23, 2024 | #Python | 2 minutes Read
One common error you might encounter while working with Python is TypeError: can't multiply sequence by non-int of type 'list'
.
This error message might initially seem complicated, but it’s straightforward once you understand what Python expects when performing multiplication with sequences.
Continue reading, so I can show you what causes this error and how you can resolve it.
What Causes TypeError: can't multiply sequence by non-int of type 'list'
?
The error, TypeError: can't multiply sequence by non-int of type 'list'
occurs when you try to multiply a sequence (such as a list or a string) by something that is not an integer.
That’s because, in Python, sequences like lists, tuples, and strings can only be multiplied by integers. Multiplying a sequence by an integer repeats that sequence multiple times. Here’s an example:
However, if you try to multiply a sequence by a list, Python will raise a TypeError
exception because it doesn’t know how to handle this operation.
Here’s a common situation where this error occurs:
The error can also occur with tuples:
Or with strings:
How to Fix TypeError: can't multiply sequence by non-int of type 'list'
To fix this error, make sure you are multiplying sequences by integers only.
Here’s how you can correct it if you’re working with a list:
Here’s how you can fix it if you’re working with tuples:
And here’s how you can fix it while working with strings:
Thank you for reading!