2

I have a set

set1= { '1', '2' }

I want to convert '1', '2' to int , like this :

set_new = { 1, 2 }

How can I do this in python?

3

2 Answers 2

11

Use map function

Demo:

>>> set1= { '1', '2' }
>>> set2 = set(map(int, set1))
>>> set2
set([1, 2])
0
0

You can use set comprehension:

>>> set1={'1', '2'}
>>> set2={int(item) for item in set1}
>>> set2
{1, 2}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.