I have a list of tuples and I want to print flattened form of this list. I don't want to transform the list, just print it without parenthesis and brackets.
input: [ ("a", 1), ("b",2), ("c", 3)]
output: a 1 b 2 c 3
Here's what I do:
l = [ ("a", 1), ("b",2), ("c", 3)]
f = lambda x: " ".join(map(str,x))
print " ".join(f(x) for x in l)
I'm interested in if anybody has a more elegant and possibly a more efficient solution,possibly without doing join, only print. Thanks in advance.
from __future__ import print_function
l = [("a", 1), ("b",2), ("c", 3)]
print(*(i for j in l for i in j))
a 1 b 2 c 3
Or using itertools.chain to flatten:
from itertools import chain
print(*chain(*l))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments