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?
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?
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
.