Python – Split the list index into a new list python

Split the list index into a new list python… here is a solution to the problem.

Split the list index into a new list python

I have an introduction to a programming lab using Python. I would like to split a list :

items = ['40/40', '10/40', '30/40', '4/5', '18/40', '40/40', '76/80', '10/10']

Go to two new lists:

items_1 = ['40','10','30','4','18','40','76','10']

items_2 = ['40','40','40','5','40','40','80','10']

Thanks for any help.

Solution

This is a standard zip one-line code. It works when items are not empty.

items = ['40/40', '10/40', '30/40', '4/5', '18/40', '40/40', '76/80', '10/10']

items_1, items_2 = map(list, zip(*(i.split('/') for i in items)))

If you are satisfied with the tuple instead of the list, you can remove the map(list(..)) structure.

Related Problems and Solutions