Reputation: 41
I have a list and want to split each elements into a tuple of two elements.
The list looks like:
list_doctors = ['dr.naman_5','dr.akanksha_7','dr.sumant_3']
How do I create a list of the form:
modified_list = [('dr.naman','5'),('dr.akanksha','7'),('dr.sumant','3')]
Upvotes: 0
Views: 1175
Reputation: 49318
Use split()
.
>>> list_doctors = ['dr.naman_5','dr.akanksha_7','dr.sumant_3']
>>> modified_list = [item.split('_') for item in list_doctors]
>>> modified_list
[['dr.naman', '5'], ['dr.akanksha', '7'], ['dr.sumant', '3']]
Upvotes: 0
Reputation: 6357
Try the following.
>>> list_doctors = ['dr.naman_5','dr.akanksha_7','dr.sumant_3']
>>> [tuple(s.split('_')) for s in list_doctors]
[('dr.naman', '5'), ('dr.akanksha', '7'), ('dr.sumant', '3')]
Upvotes: 2