Categories
Python Answers

How to join a list of items with different types as string in Python?

Spread the love

Sometimes, we want to join a list of items with different types as string in Python.

In this article, we’ll look at how to join a list of items with different types as string in Python.

How to join a list of items with different types as string in Python?

To join a list of items with different types as string in Python, we convert all the items in the list into strings before calling join.

For instance, we write

s = ', '.join(map(str, my_list))

to call map with str and my_list to return an iterable with all the items in my_list converted to strings.

Then we call ', '.join with the iterable to join the strings with ', ' and return it.

Conclusion

To join a list of items with different types as string in Python, we convert all the items in the list into strings before calling join.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *