Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input int list in Python 3 [duplicate]

I tried to change from Python 2.7 to 3.3.1

I want to input

1 2 3 4

and output in

[1,2,3,4]

In 2.7 I can use

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

What should I use in Python 3.x?

like image 854
lifez Avatar asked May 13 '13 15:05

lifez


People also ask

Can you have duplicate values in a list Python?

Python list can contain duplicate elements.


2 Answers

Use input() in Python 3. raw_input has been renamed to input in Python 3 . And map now returns an iterator instead of list.

score = [int(x) for x in input().split()]

or :

score = list(map(int, input().split()))
like image 105
Ashwini Chaudhary Avatar answered Oct 06 '22 00:10

Ashwini Chaudhary


As a general rule, you can use the 2to3 tool that ships with Python to at least point you in the right direction as far as porting goes:

$ echo "score = map(int, raw_input().split())" | 2to3 - 2>/dev/null
--- <stdin> (original)
+++ <stdin> (refactored)
@@ -1,1 +1,1 @@
-score = map(int, raw_input().split())
+score = list(map(int, input().split()))

The output isn't necessarily idiomatic (a list comprehension would make more sense here), but it will provide a decent starting point.

like image 36
Cairnarvon Avatar answered Oct 06 '22 01:10

Cairnarvon