Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] The Most Pythonic Way to Convert a List of Tuples to a String
1
The Most Pythonic Way to Convert a List of Tuples to a String

[Image: convert-1024x576.jpg]

The most Pythonic way to convert a list of tuples to a string is to use the built-in method
Code:
str(...)
. If you want to customize the delimiter string, the most Pythonic way is to concatenate the
Code:
join()
method and the
Code:
map()
function
Code:
'\n'.join(map(str, lst))
to convert all tuples to strings and gluing those together with the new-line delimiter
Code:
'\n'
.




Exercise: Run the interactive code snippet. Which method do you like most?

https://trinket.io/embed/python/ad8db38a29

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 thisSO post). The easiest way to accomplish this is to use the default string conversion method
Code:
str(...)
.

Code:
>>> 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 delimiterstring, you can use the
Code:
join()
method in combination with the
Code:
map()
function
:

Code:
lst = [(1,1), (2,1), (4,2)] print('--'.join(map(str, lst))) # (1, 1)--(2, 1)--(4, 2)

The
Code:
map()
function transforms each tuple into a string value, and the
Code:
join()
method transforms the collectionof strings to a single string—using the given delimiter
Code:
'--'
. If you forget to transform each tuple into a string with the
Code:
map()
function, you’ll get a
Code:
TypeError
because the
Code:
join()
method expects a collection of strings.

Method 3: Flatten List of Tuples

If you want to flatten the list and integrate all tupleelements into a single large collection of elements, you can use a simple list comprehension statement
Code:
[str(x) for t in lst for x in t]
.

Code:
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
Code:
join()
method and the
Code:
map()
function with a custom lambda function
Code:
lambda x: str(x[0]) + ' ' + str(x[1])
to be applied to each list element.

Code:
lst = [(1,1), (2,1), (4,2)] print('\n'.join(map(lambda x: str(x[0]) + ' ' + str(x[1]), lst))) ''' 1 1 2 1 4 2 '''
Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: