chattrat423 chattrat423 - 4 months ago 24
Python Question

Convert List to Rows

What is an efficient way to convert a list into separate elements in python?

I have a dataset that looks like this;

['StudentA','80','94','93']
['StudentB','93','94']


I would like to reshape the data so that each student/score has its own row;

['StudentA','80']
['StudentA','94']
etc...

Answer

You could use a list comprehension, like this:

data = ['StudentA','80','94','93']
res = [[data[0], x] for x in data[1:]]

This sets res to [['StudentA', '80'], ['StudentA', '94'], ['StudentA', '93']].