1

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

2
  • 1
    Are a and b strings or variables? Commented Sep 30, 2016 at 16:34
  • @AndrewL. all strings. Commented Sep 30, 2016 at 19:01

1 Answer 1

1

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])))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.