1

I have a list like as follows

list1 = [['abc',{2:1,3:4,5:6}],['xyz',{4:0,9:5,7:8}],.......]]

I want write the list in a o/p file in the following format:

'abc' 2:1 3:4 5:6
'xyz' 4:0 9:5 7:8

I tried different ways, but not able to get it in the above format.Let me also mention that len(list1) = 30000.Please suggest me an optimized way

1
  • 4
    so you want the output to have only one long line? by the way, please post your code you are using so that we can help you instead of us writing code for you. Commented Sep 27, 2015 at 9:02

1 Answer 1

2
list1 = [['abc', {2: 1, 3: 4, 5: 6}], ['xyz', {4: 0, 9: 5, 7: 8}]]

with open('some.txt', 'w') as output_file:
    for k, v in list1:
        output_file.write('\'{}\' {}\n'.format(
            k, ' '.join('{}:{}'.format(key, val) for key, val in v.items())))

For Python 3 just use v.items().

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.