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