Transform list of strings into list of tuples

4

I have this:

l=['Car 8', 'Bike 5', 'Train 10']

And I would like to have something like this:

[('Car','8'), ('Bike','5'), ('Train','10')]

How could you make a list of strings into something like a list of strings tuples?

    
asked by anonymous 05.12.2017 / 01:27

1 answer

6

By using list comprehensions you can do:

>>> [tuple(x.split(' ')) for x in l]
>>> [('Car', '8'), ('Bike', '5'), ('Train', '10')]

Each element is made split with space and on the result of split , which is a list, the tuple is constructed using tuple .

See the code running on Ideone

    
05.12.2017 / 02:10