Lets say I have a list of students, a list of test scores, and a list of averages. How do I write each of these lists to one file formatted so that the first student, test scores, and average will be on line one, the second student, test scores, and average will be on line two, etc.?
asked Nov 12, 2013 at 4:51
3
Try this:
with open["file.txt","w"] as f:
for [student,score,avg] in zip[students,scores,avgs]:
f.write["{0},{1},{2}\n".format[student,score,avg]]
If you don't want to use a for loop, you could look at csv.writer, but that's hardly more "streamlined" than what I've posted here.
answered Nov 12, 2013 at 4:54
kevinsa5kevinsa5
3,1712 gold badges24 silver badges28 bronze badges
6
Without a for loop
with open["file.txt", "w"] as f:
f.writelines[map["{},{},{}\n".format, students, scores, avgs]]
answered Nov 12, 2013 at 4:57
John La RooyJohn La Rooy
286k51 gold badges358 silver badges498 bronze badges
4
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In real applications, we often have to work with multiple lists, and initialize them with empty lists hampers the readability of code. Hence a one-liner is required to perform this task in short so as to give a clear idea of the type and number of lists declared to be used.
Method #1: Using loops
We can enlist all the required list comma separated and then initialize them with a loop of empty lists.
Python3
list1, list2, list3, list4
=
[[]
for
i
in
range
[
4
]]
print
[& quot
The initialized lists are : & quot
]
print
[& quot
List
1
: & quot
+
str
[list1]]
print
[& quot
List
2
: & quot
+
str
[list2]]
print
[& quot
List
3
: & quot
+
str
[list3]]
print
[& quot
List
4
: & quot
+
str
[list4]]
Output:
The initialized lists are : List 1 : [] List 2 : [] List 3 : [] List 4 : []
Method #2: Using defaultdict[] Method
This is a method different and also performs a slightly different utility than the above two methods discussed. This creates a dictionary with a specific name and we have the option to make any number of keys and perform the append operations straight away as they get initialized by the list.
Python3
import
collections
mul_list_dict
=
collections.defaultdict[
list
]
mul_list_dict[
'list1'
].append[
1
]
mul_list_dict[
'list2'
].append[
2
]
mul_list_dict[
'list3'
].append[
3
]
mul_list_dict[
'list4'
].append[
4
]
print
[& quot
The initialized lists are : & quot
]
print
[& quot
List
1
: & quot
+
str
[mul_list_dict[
'list1'
]]]
print
[& quot
List
2
: & quot
+
str
[mul_list_dict[
'list2'
]]]
print
[& quot
List
3
: & quot
+
str
[mul_list_dict[
'list3'
]]]
print
[& quot
List
4
: & quot
+
str
[mul_list_dict[
'list4'
]]]
Output:
The initialized lists are : List 1 : [1] List 2 : [2] List 3 : [3] List 4 : [4]
Method 4: Using * operator:
Itdoes not create independent lists, but variables referring to the same [empty] list!
Python3
list1, list2, list3, list4
=
[[], ]
*
4
list1.append[
"hello there"
]
print
[& quot
The initialized lists are
all
the same: & quot
]
print
[& quot
List
1
: & quot
+
str
[list1]]
print
[& quot
List
2
: & quot
+
str
[list2]]
print
[& quot
List
3
: & quot
+
str
[list3]]
print
[& quot
List
4
: & quot
+
str
[list4]]
Output:
The initialized lists are all the same: List 1 : ["hello there"] List 2 : ["hello there"] List 3 : ["hello there"] List 4 : ["hello there"]