using map(int, raw_input().split())

80,783

Solution 1

If you're using map with built-in function then it can be slightly faster than LC:

>>> strs = " ".join(str(x) for x in xrange(10**5))
>>> %timeit [int(x) for x in strs.split()]
1 loops, best of 3: 111 ms per loop
>>> %timeit map(int, strs.split())
1 loops, best of 3: 105 ms per loop

With user-defined function:

>>> def func(x):
...     return int(x)

>>> %timeit map(func, strs.split())
1 loops, best of 3: 129 ms per loop
>>> %timeit [func(x) for x in strs.split()]
1 loops, best of 3: 128 ms per loop

Python 3.3.1 comparisons:

>>> strs = " ".join([str(x) for x in range(10**5)])
>>> %timeit list(map(int, strs.split()))
10 loops, best of 3: 59 ms per loop
>>> %timeit [int(x) for x in strs.split()]
10 loops, best of 3: 79.2 ms per loop

>>> def func(x):                         
    return int(x)
... 
>>> %timeit list(map(func, strs.split()))
10 loops, best of 3: 94.6 ms per loop
>>> %timeit [func(x) for x in strs.split()]
1 loops, best of 3: 92 ms per loop

From Python performance tips page:

The only restriction is that the "loop body" of map must be a function call. Besides the syntactic benefit of list comprehensions, they are often as fast or faster than equivalent use of map.

Solution 2

List comprehensions!

Intuitive and pythonic:

a = [int(i) for i in raw_input().split()]

Check out this discussion here: Python List Comprehension Vs. Map

Solution 3

You can use this:

s = raw_input().split()
s = [int(i) for i in s]
Share:
80,783
Aswin Murugesh
Author by

Aswin Murugesh

Head of Engineering at Ongil Pvt Ltd contact me : facebook Twitter mail: [email protected]

Updated on July 13, 2022

Comments

  • Aswin Murugesh
    Aswin Murugesh almost 2 years

    Though I like python very much, When I need to get multiple integer inputs in the same line, I prefer C/C++. If I use python, I use:

    a = map(int, raw_input().split())
    

    Is this the only way or is there any pythonic way to do it? And does this cost much as far as time is considered?

  • Aswin Murugesh
    Aswin Murugesh almost 11 years
    So mapping is the fastest way to do it?
  • Ashwini Chaudhary
    Ashwini Chaudhary almost 11 years
    Yes, for builtn-in functions it is generally faster, but LC are considered more pythonic and will work in both py2x and py3x. map returns an iterator in py3x.
  • jeromej
    jeromej almost 11 years
    @AswinMurugesh Not always, but remember that list comprehensions & co are a map + a filter with two lambdas. If you plan only using a map with an internal function then map will be slightly faster than list comprehension. But when you DO mix all these, list comprehension do it better than you can do it yourself (if all that makes sense to you).
  • Aswin Murugesh
    Aswin Murugesh almost 11 years
    I can't get you. What do you mean by mix all these?
  • jeromej
    jeromej almost 11 years
    @AswinMurugesh Using map with a lambda + a filter with a lambda function. Because doing [e+1 for e in foo if e%3] is the same than map(lambda e: e+1, filter(lambda e: e%3, foo)) but LC are faster in that case (and more Pythonic, isn't it?)
  • jeromej
    jeromej almost 11 years
    @AswinMurugesh Don't forget to add @ followed by thePersonYouAreTalkingTo when it isn't the author of the answer but another commentator, otherwise I'm not noticed and I could have never seen your questions/answers.
  • Ashwini Chaudhary
    Ashwini Chaudhary almost 11 years
    @AswinMurugesh If you're using this is in a programming competition then I'll also suggest you to use sys.std.readline instead of raw_input.
  • Nic3500
    Nic3500 over 5 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.