Python

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.

joinExample.py

tmpList = ["food", "music", "car", "work", "dance"]

output = " and ".join(tmpList)

print(output)
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.

joinExample.py

multiList = ["food" , 666 , 12.3 , "dance"]

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

print(output)
  • 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.

joinExample.py

tmpString = "Potato"

output = " , ".join(tmpString)

print(output)
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.

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

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button