Code Snippets

Convert List, Strings and Tuples

January 26, 2020 by Michael Lossagk , 1 min  • 
Convert List, Strings and Tuples

1. Introduction

It is very easy to convert lists, strings and tuples in Python. In this tutorial I will show you how you can do this.

2. List to String

First we have a look how easy it is to convert a list to a string

myIntegerList = [1, 2, 3, 4, 5]
myStringList = ["One", "Two", "Three", "Four", "Five"]
myMixedList = ["One", 2, "Three", 4, "Five"]

# List to String. Works if all items are strings
print(" ".join(myStringList))

# does not work

The output is

One Two Three Four Five

3 Tuple to string

Now we want to convert tuples to strings.

myTuple = [('Vanilla',), ('Sugar',), ('Garlic',)]
print(", ".join(map(lambda x: x[0], myTuple)))

The output is

Vanilla, Sugar, Garlic

That was easy, wasn’t it?

Michael Lossagk
Michael Lossagk
Coding Enthusiast
Founder @ TechDiffuse

You may also like

Share this: