NARAYAN CHANGDER
NARAYAN CHANGDER

Reputation: 319

How to Convert list of string to tuple in python

I am facing a problem to convert from List to tuple. For example i have lists like this

['1', '9']
['2']
['3']
['4']

I want output like this [(1,9),(2),(3),(4)] Kindly help me. I am not getting any way.

Upvotes: 1

Views: 3316

Answers (1)

gipsy
gipsy

Reputation: 3859

If you have a list 'l' then you can call the builtin function 'tuple' on it to convert to a tuple

l = [1,2]
tup = tuple(l)
print tup # (1,2)

if you have a list of list l = [['1', '9'], ['2'], ['3'], ['4']]

you can do :

l = [['1', '9'], ['2'], ['3'], ['4']]
tups = map(lambda x: tuple(x), l)
print tups #  [(1,9),(2),(3),(4)] 

Upvotes: 2

Related Questions