Home > OS >  How can i convert list to a tuple inside a tuple
How can i convert list to a tuple inside a tuple

Time:10-01

input:

(('Alfred', ['gaming', 'shopping', 'sport', 'travel']), 
 ('Carmen', ['cooking', 'pets', 'photography', 'shopping', 'sport',]))

How can i convert that list to a tuple?

expected output:

(('Alfred', ('gaming', 'shopping', 'sport', 'travel')), 
 ('Carmen', ('cooking', 'pets', 'photography', 'shopping', 'sport',)))

CodePudding user response:

I'm going to assume your original data is in a variable named t

t = tuple((item[0], tuple(item[1])) for item in t)

This uses tuple() to convert the list (or any iterable) into a tuple.

Docs: https://docs.python.org/3.3/library/stdtypes.html?highlight=tuple#tuple

  • Related