The most Pythonic way to convert a list of tuples to a string is to use the built-in method
str(...). If you want to customize the delimiter string, the most Pythonic way is to concatenate the
join() method and the
'\n'.join(map(str, lst)) to convert all tuples to strings and gluing those together with the new-line delimiter
Exercise: Run the interactive code snippet. Which method do you like most?
Method 1: Default String Conversion
Say, you’ve got a list of tuples, and you want to convert it to a string (e.g., see this SO post). The easiest way to accomplish this is to use the default string conversion method
>>> lst = [(1,1), (2,1), (4,2)] >>> str(lst) '[(1, 1), (2, 1), (4, 2)]'
The result is a nicely formatted string representation of your list of tuples.
Method 2: Join() and Map()
However, if you want to customize the delimiter string, you can use the
join() method in combination with the
lst = [(1,1), (2,1), (4,2)] print('--'.join(map(str, lst))) # (1, 1)--(2, 1)--(4, 2)
map() function transforms each tuple into a string value, and the
join() method transforms the collection of strings to a single string—using the given delimiter
'--'. If you forget to transform each tuple into a string with the
map() function, you’ll get a
TypeError because the
join() method expects a collection of strings.
Method 3: Flatten List of Tuples
If you want to flatten the list and integrate all tuple elements into a single large collection of elements, you can use a simple list comprehension statement
[str(x) for t in lst for x in t].
lst = [(1,1), (2,1), (4,2)] print('\n'.join([str(x) for t in lst for x in t])) ''' 1 1 2 1 4 2 '''
If you want to redefine how to print each tuple—for example, separating all tuple values by a single whitespace character—use the following method based on a combination of the
join() method and the
map() function with a custom lambda function
lambda x: str(x) + ' ' + str(x) to be applied to each list element.
lst = [(1,1), (2,1), (4,2)] print('\n'.join(map(lambda x: str(x) + ' ' + str(x), lst))) ''' 1 1 2 1 4 2 '''
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.