Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

I'm new to programming, and I need to know how to read an integer from a single line of input in python. 

2 4 6 8 10

I have tried two methods

1.

arr = input.split(' ')

But this will create an array of string instead of converting them to integers

2.

arr = input.split(' ')

for i,val in enumerate(arr): arr[i] = int(val)

This one works fine, but it would be more perfect if it is a single line solution

please help me !!

1 Answer

0 votes
by (26.4k points)
edited by

You can use a map :-

arr = list(map(int, input().split()))

This code works fine for both 2.x Python and 3.x Python

Want to be a Python expert? Come and join this python course.

If you want to know more about these topics, you can also look at the following video tutorial :

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...