Skip to content Skip to sidebar Skip to footer

Python - Nested List To Tab Delimited File?

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]. I wish to create a function in order to output

Solution 1:

with open('fname', 'w') as file:
    file.writelines('\t'.join(i) + '\n' for i in nested_list)

Solution 2:

>>> nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]
>>> for line in nested_list:
...   print '\t'.join(line)
... 
x   y   z
a   b   c
>>> 

Solution 3:

In my view, it's a simple one-liner:

print '\n'.join(['\t'.join(l) for l in nested_list])

Solution 4:

>>> print '\n'.join(map('\t'.join,nested_list))
x       y       z
a       b       c
>>>

Solution 5:

out = file("yourfile", "w")
for line in nested_list:
    print >> out, "\t".join(line)

Post a Comment for "Python - Nested List To Tab Delimited File?"