Nested list in tab delimited separator file?

I have a nested list containing ~ 30,000 subscriptions, each with three entries, e.g.

nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']].

I want to create a function to output this data construct in tab delimited format, e.g.

x    y    z
a    b    c

Any help is much appreciated!

Thanks in advance, Seafoid.

+5
source share
5 answers
with open('fname', 'w') as file:
    file.writelines('\t'.join(i) + '\n' for i in nested_list)
+5
source
>>> nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]
>>> for line in nested_list:
...   print '\t'.join(line)
... 
x   y   z
a   b   c
>>> 
+6
source

In my opinion, this is a simple one-line:

print '\n'.join(['\t'.join(l) for l in nested_list])
+4
source
>>> print '\n'.join(map('\t'.join,nested_list))
x       y       z
a       b       c
>>>
+2
source
out = file("yourfile", "w")
for line in nested_list:
    print >> out, "\t".join(line)
+1
source

All Articles