TypeError: can only concatenate list (not "str") to list

1 6
calendar_today agoschedule3 min read

Today, we're going to discuss a TypeError in Python: “can only concatenate list (not "str") to list. It occurs when we're trying to concatenate a string value to a list, which is invalid operation in Python. In this study blog, we'll go through what is TypeError: can only concatenate list (not "str") to list, the main causes and how to resolve it. Let's begin.

What is "TypeError: can only concatenate list (not "str") to list" #

This error occurs when we're trying to merge a list with a string value. Let's look an example:


fruits = ["apple", "orange", "banana"]
fruit = "pineapple"

print(fruits + fruit)
  

This is the output when we run the code:


Traceback (most recent call last):
  File "C:\Users\MC\IdeaProjects\programming solution\sample.py", line 4, in 
    print(fruits + fruit)
TypeError: can only concatenate list (not "str") to list
  

It's normal because we're doing invalid operation, which are concatenating two values with different data types. But why? we'll explore it in the next section.

Causes #

In Python, if we want to use "+" operator to concatenate two values, both the values must have the same data type; else, it will throw TypeError at the output. Let's say:


fruits = ["apple", "orange", "banana"]
fruit = ["pineapple"]

print(fruits + fruit)
  

This is the output:


['apple', 'orange', 'banana', 'pineapple']
  

You may notice that "pineapple" in fruit is concatenated successfully to fruits. Why? Because both of them are having same data type, that's list. Don't worry, there're still couple of simple ways to omit this error, we'll see them next.

Solutions #

Here, we're going to use some different solutions to resolve this problem. Let's understand each of them:

Solution 1: Using [ ] #

Using [ ] is the most direct and simplest way to concatenate a string value to a list. Let's look an the example:


fruits = ["apple", "orange", "banana"]
fruit = "pineapple"

print(fruits + [fruit])
  

Here, we'd put a a string value, "pineapple" directly in a [ ] so that it can be converted directly to a list. Therefore, the concatenation will not throw the error.

Tip [ ] is useful if you want to add multiple values into a list

For example:


fruits = ["apple", "orange", "banana"]

print(fruits + ["strawberry", "pineapple"])
  

Here's the output:


['apple', 'orange', 'banana', 'strawberry', 'pineapple']
  

Solution 2: Using append() function #

Using append() function is similar to using a [ ]. For example:


fruits = ["apple", "orange", "banana"]
fruit = "pineapple"
fruits.append(fruit)

print(fruits)
  

This is the output:


  ['apple', 'orange', 'banana', 'pineapple']
  

It allows us to add a value to the end of the list.

Note append() function will mutate the original list. Hence, please make sure you make a copy of your original list if you still need it. For example, you may use copy() to make a copy of your original list and store at other place.
Tip If you want to concatenate two lists together, you can also use extend() function. Note that it'll also mutate original list.

The Conclusion #

Today, we've discuss about TypeError: can only concatenate list (not "str") to list in Python, why it occurs and how to resolve it easily. We can use [ ] to convert a string value to a list value for concatenation. We also can use append() function to add a value directly to the end of a list. Thanks for following us until the end and have a nice day! :)

The Reference #

You may go through this reference for more information:

  1. TypeError: https://docs.python.org/3/library/exceptions.html?highlight=typeerror#TypeError
  2. append() in Python: https://docs.python.org/3/library/array.html?highlight=append#array.array.append
  3. Copy a list in Python: https://docs.python.org/3/library/copy.html?highlight=copy#module-copy
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Why Are There Only 13 DNS Root Servers For The Whole World? Is that a problem

richarddjarbeng - May 7

Typeerror: sequence item 0: expected str instance, list found

Mark Thompson - Nov 27, 2023

AWS Certifications Are a Building Block, Not the Final Destination

Ijay - Jun 16
chevron_left
136 Points7 Badges
1Posts
3Comments
I'm a software developer who enjoys turning ideas into practical, working solutions. I like building... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!