michael
michael

Reputation: 65

How can I convert a set of numeric strings to a set of integers?

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?

Upvotes: 2

Views: 10208

Answers (2)

Serge Sotnyk
Serge Sotnyk

Reputation: 123

You can use set comprehension:

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

Upvotes: 0

Vivek Sable
Vivek Sable

Reputation: 10223

Use map function

Demo:

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

Upvotes: 11

Related Questions