7

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?

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
lifez
  • 318
  • 1
  • 4
  • 9

2 Answers2

28

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()))
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

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.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65