Python Join List Example

In this article, we will explain how to use the function Join List in Python through examples.

1. Introduction

Python join() function means concatenating an iterable of strings with a specified delimiter to form a string. Sometimes it’s useful when you have to convert an iterable to a string.

2. How to Use join list in Python

The syntax of the join function is str.join(iterable) where str is the specified delimiter. Also, TypeError will be raised if there are any non-string values in iterable, including bytes objects.
tmpList = ["food", "music", "car", "work", "dance"]

output = " and ".join(tmpList)

python join list - join() Function Example Output
Fig. 1: How to Use join() Function Example Output.

3. Why join() Function is in String and not in List?

This is a question that many python developers have. The most important point from the discussions on the internet is because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the delimiter must be strings. That means that is harder to program and optimize the join() function for all the different iterable objects in order to work. For that reason is much simpler to be a String method and not an Iterable method.

4. Joining List of Multiple data-types Example

Because join() function accepts only iterable with String Elements, in order to join a List of multiple data-types, we must cast its element into a string. Lets see an example.
multiList = ["food" , 666 , 12.3 , "dance"]

output = " and ".join(map(str, multiList))

  • line 1: We create a list with multiple data-types.
  • line 3: We use as before the join() function but this time we use the map() function in order to cast its element into a string. The map() function returns an iterable that applies str to every item of multilist.
python join list - Joining List of Multiple data-types Example Output.
Fig. 2: Joining List of Multiple data-types Example Output.

5. Split String Using join() Function Example

We can use the join() function in order to split a string to its of its letters.
tmpString = "Potato"

output = " , ".join(tmpString)

python join list - Split String Using join() Function Example Output.
Fig. 3: Split String Using join() Function Example Output.

6. Summary

As we can see from the examples above, the join() method provides a flexible way to create strings from iterable objects.

8. Download Source Code

This was an example of how we can use the join() function in python.

You can download the full source code of this example here: Python Join List Example

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button