0

I have read the contents of a file into a list by reading each line as a new element. Now I want to separate these further into their own sublists. For example, the first value before the first comma in each string is a date, so I want to make a new list with only the dates from this given list, and so on. How can I go about doing this?

['2019/01/11,1640.5600,4670093.0000,1640.5500,1660.2940,1636.2200', '2019/01/10,1656.2200,6487252.0000,1641.0100,1663.2500,1621.6150', '2019/01/09,1659.4200,6333528.0000,1652.9800,1667.7989,1641.3953',  '2014/01/13,390.9800,2843810.0000,397.9800,399.7800,388.4500']

I've tried using list.split to separate the values indicated by commas, but I can't get it to work on this whole list.

mtrns
  • 73
  • 6
  • I think you would have to loop through each of the values of the `list` with a `for-loop`. Look at the documentation [here](https://docs.python.org/3/tutorial/controlflow.html#for-statements) for more information. – Hampus Larsson Sep 02 '19 at 01:29

1 Answers1

1
lines = [
    '2019/01/11,1640.5600,4670093.0000,1640.5500,1660.2940,1636.2200',
    '2019/01/10,1656.2200,6487252.0000,1641.0100,1663.2500,1621.6150',
    '2019/01/09,1659.4200,6333528.0000,1652.9800,1667.7989,1641.3953',
    '2014/01/13,390.9800,2843810.0000,397.9800,399.7800,388.4500',
]

dates = [line.split(',')[0] for line in lines]
print(dates)