16

I am trying to sort array in increasing order. But getting the following error for the code:

a = []
a = map(int, input().split(' '))
a.sort()
print(a)

Error:

AttributeError: 'map' object has no attribute 'sort'

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Sushil Chaskar
  • 181
  • 1
  • 1
  • 8
  • 2
    This is not related to the question, but `split()` (i.e. without arguments) is a better choice than `split(' ')`. – VPfB Oct 18 '15 at 17:24

1 Answers1

30

In python 3 map doesn't return a list. Instead, it returns an iterator object and since sort is an attribute of list object, you're getting an attribute error.

If you want to sort the result in-place, you need to convert it to list first (which is not recommended).

a = list(map(int, input().split(' ')))
a.sort()

However, as a better approach, you could use sorted function which accepts an iterable and return a sorted list and then reassign the result to the original name (is recommended):

a = sorted(map(int, input().split(' ')))
Mazdak
  • 105,000
  • 18
  • 159
  • 188