0
x='2013:02:01'

y,m,d=x.split(':')

Produces y,m,d as strings. But how do I produce them as ints using only 1 line

Failed:

y,m,d=int(y.split(':'))


y,m,d=int(y),int(m),int(d)=y.split(':')

4 Answers 4

7
y, m, d = map(int, x.split(':'))

map applies the function to each of the elements in the iterable. In this case it will apply int to each of the values returned by split and give you the result.

Sign up to request clarification or add additional context in comments.

1 Comment

beat me by 3 seconds ;)
3

Use a list comprehension:

[ int(token) for token in y.split(':') ]

Comments

3

You can use list comprehensions:

y,m,d = [int(n) for n in x.split(':')]

Comments

2

Using list comprehension will do the trick:

>>> x='2013:02:01'
>>> y,m,d=[int(n) for n in x.split(':')]
>>> y
2013
>>> d
1
>>> m
2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.