-1

Below is the code

from __future__ import print_function

upper = []
lower = []
even = []
odd = []

def separator(a):
    print(a)
    if a.isalpha():
        if a.isupper():
            upper.append(a)
        else:
            lower.append(a)
    else:
        if int(a)%2 == 0:
            even.append(a)
        else:
            odd.append(a)
    return 
list = ['S','o','t','']   
map(separator,list)       

upper.sort()
lower.sort()
even.sort()
odd.sort()

t = lower+upper+odd+even
print(t)

Here when I run the code, It works fine on python 2 but same doesn't works on python 3. seperator function is not getting called from map.

Sujay
  • 139
  • 1
  • 9

2 Answers2

0

In Python 3, map returns an iterator – it will only call the function on each element as you iterate over the result. To get around this, you can cast the result of map to a tuple or list so that the iterator gets consumed (and thus, executed).

tuple(map(separator,list))

Note, however, that your code is pretty bad style – map should (generally) produce new data as opposed to editing global variables.

Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • Note: The Pythonic replacement for `map` here would just be `for x in list: separator(x)`. Of course, you also wouldn't name a variable `list` (particularly in global scope, since it shadows the `list` constructor from there on for all code executed after that line). – ShadowRanger Nov 02 '16 at 05:35
0

map works differently in Python 2 than in Python 3. In Python 2, the map will run and produce a list. In Python 3, it simply returns an iterator, which you will have to iterate over to get the results.

Using map the way you have here is not really idiomatic anyway, so I would suggest that you just use a "normal" loop.

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66