Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use map() function with string

Is there a way to use map() function with a string instead of a list? Or the map() function is meant only to work with lists?

For instance, ignoring the content of the lambda function, this code returns a map object and not a string:

def rot_13(string):
    alph = 'abcdefghijklmnopqrstuwxyz'
    return str(map(lambda i: alph[(alph.find(i)+13) % len(alph)], string))
like image 262
bertonc96 Avatar asked Aug 26 '26 20:08

bertonc96


1 Answers

Using list/generator comprehensions is preferable (more Pythonic) to map():

def rot_13(string):
    alph = 'abcdefghijklmnopqrstuwxyz'
    return ''.join(alph[(alph.find(i)+13) % len(alph)] for i in string)
like image 111
AKX Avatar answered Aug 29 '26 09:08

AKX