Naman Arora
Naman Arora

Reputation: 41

How to break a python list tuple into tuple with two elements?

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

Answers (2)

TigerhawkT3
TigerhawkT3

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

Abdul Fatir
Abdul Fatir

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

Related Questions