

Now we want to transform the values in dictionary by appending ‘_’ at the end of each value i.e. Modified List : Using map() function to transform Dictionaries in Python ModifiedList = list(map(lambda x, y: x * y, listOfNum, *len(listOfNum) )) Now let’s multiply each element in lists by 2 and get a new lists of updated values using map() i.e. Then Values returned by lambda function are stored in a new list and returned in the end.īoth the lists passed to map() function should be of same size otherwise it will through error. map() function iterates over both the lists simultaneously and pass their each element to lambda function. Here passed lambda function accepts two arguments and returns a new string by join the passed arguments. ModifiedList = list(map(lambda x, y: x + '_' + str(y), list1, list2)) To do that we can pass both the lists and a lambda function in map() i.e. Now we want to join elements from list1 to list2 and create a new list of same size from these joined lists i.e. Map() function and get a joined list based on them. The map() function, along with a function as argument can also pass multiple sequence like lists as arguments. Passing multiple arguments to map() function in Python In the end it returned a new string with modified content. Map() function iterated over each character in the given string and applied the given lambda function on each character to increment it’s ASCII value by 1. # increment ascii value of each character by 1 in the stringĮncryptedText = ''.join(map(lambda x: chr(ord(x) + 1), sampleStr)) Now let’s transform this string by increment each character by 1 in it’s ascii value i.e. Let’s see how to transform strings using map(), We can also convert a string to another format using map() function because string is also an iterable sequence of characters. Convert a string to other format using map() function in Python Then in last returns the new sequence of reversed string elements. Then stores the value returned by lambda function to a new sequence for each element. It iterates over the list of string and apply lambda function on each string element. ModifiedList = list(map(lambda x : x, listOfStr)) # Reverse each string in the list using lambda function & map() Let’s use lambda function to reverse each string in the list like we did above using global function, In the map() function along with iterable sequence we can also the lambda function. Then in last returns the new sequence of int i.e length of each string element. Then stores the length returned by len() in a new sequence for every element. It iterates over the list of string and apply len() function on each string element. # Get a list of int from a list of string We can also use the map() function to convert a list of strings to a list of numbers representing the length of each string element i.e.

In the end it returns the new sequence with reversed strings. Also stores the value returned by reverseStr() in a sequence. It iterates over all the string elements in our listĪnd calls reverseStr() for each string element. Modified List : Īs arguments, map() functions accepts our original list of string and a function reverseStr(). ModifiedList = list(map(reverseStr, listOfStr))

Now let’s convert each string element in the list using map() function i.e. Now let’s see some examples of map() function, Use map() function with list of strings and a global function
