writing list contents to a file

Paul Simmonds psimmo60 at hotmail.com
Fri Dec 13 05:16:59 EST 2002


>From: Christoph Lehmann <lehmann at puk.unibe.ch>
>To: python-list at python.org
>Subject: writing list contents to a file
>Date: Fri, 13 Dec 2002 11:03:39 +0100
>
>Hi I am a newbie:
>I have two lists each with the same length: i want to write its content
>in the way
>
>list1Item1  list2Item1
>list1Item2  list2Item2
>....
>list1Itemn  list2Itemn
>
>what's the moste elegant way?
>
<snip>

Don't whether this is particularly elegant, but the easiest way if both 
lists are the same length is to loop through the indexes:

>>>a=[1,2,3,4,5,6,7,8]
>>>b=['a','b','c','d','e','f','g','h']
>>>file=open('lists.txt','w')
>>>for index in range(len(a)):
...     file.write(str(a[index])+' '+str(b[index])+'\n')
...
>>>file.close()
>>>

HTH,
Paul

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail





More information about the Python-list mailing list