mko
mko

Reputation: 61

Writing a nested list in Python in a file

I have a list like: a = [(a,[1,2,3]),(b,[4,5,6)].

How can I write the above list in a txt file like:

a 1 2 3
b 4 5 6

in a most pythonic way? Thanks

Upvotes: 1

Views: 1305

Answers (1)

DeepSpace
DeepSpace

Reputation: 81594

This should do the trick:

with open('filename', 'w') as f:
    for elem in a:
        f.write('{} {}\n'.format(elem[0], ' '.join(str(i) for i in elem[1])))

Upvotes: 1

Related Questions