I am new to python. I want to read the input from stdin as nested list.
Stdin:
JavaScript
x
4
1
student1 90
2
student2 85
3
student3 98
4
My list should be as follows:
JavaScript
1
2
1
student = [['student1',90],['student2',85],['student3',98]]
2
Is there any way I can read the input using list comprehension without needing any extra space.
Advertisement
Answer
This is one way.
JavaScript
1
6
1
mystr = 'student1 90nstudent2 85nstudent3 98'
2
3
[[i[0], int(i[1])] for i in (k.split() for k in mystr.split('n'))]
4
5
# [['student1', 90], ['student2', 85], ['student3', 98]]
6