Python list map(7 examples)

In this post, we will understand Python’s list map(7 examples) with examples. The map() function applies a function on each element of iterable (list, tuple, set, dictionary) and transforms its elements, and returns a map object. We can convert a map object to a list by using list() or tuple using tuple(), or convert a map object to the dictionary using dict().

1. Python list map to another list


In this example, we have two lists and we are mapping one list to another list by using the list comprehension and zip method. It will map both lists into a list of tuples.

lstnum = [1, 2, 3, 4, 5]
lstStr = [['Marks'], ['Name'], ['Subj', 'Class'], ['Address', 'City']]
dat = [(item1, str) for item1, item2 in zip(lstnum, lstStr) for str in item2]
print(dat)

Output

[(1, 'Marks'), (2, 'Name'), (3, 'Subj'), (3, 'Class'), (4, 'Address'), (4, 'City')]

2. Python List map to a dictionary


In this example, we have mapped a list to a dictionary, and the map() function applied the lambda function on each element of the list and transformed it into the dictionary.

mylist = ["CSharp", "SQL",'Python','Rust']
listtodict = dict( map( lambda item: (item[0], item), mylist ))
print(listtodict)

Output

{'C': 'CSharp', 'S': 'SQL', 'P': 'Python', 'R': 'Rust'}

3. Python list map


In this example, we have defined a function lstSqr that finds the square of list elements, map() function is used to map this function to the list and return a square root of list elements. The lambda function is used to write the same code in one line.

mylist = [3,9,12,16,4]

def lstSqr(num):
    return num * num
resMapLst = list(map(lstSqr, mylist))

print(resMapLst)


#python map list using lambda
reslst = map(lambda num: num * num, mylist)

print('Using lambda:',list(reslst))

Output

[9, 81, 144, 256, 16]
Using lambda: [9, 81, 144, 256, 16]

4. Python List map of string to int


We have a list of strings that is transformed into a list of int by using the map() function by passing the data type int. The map function will return a map object, to convert the map object into a list the list() function is used.

mylist = ['3','9','12','16','4']

numbers = list(map(int, mylist))


print('map list int:',numbers)

Output

map list int: [3, 9, 12, 16, 4]

5. Python map List of int to String


We have a list of int that is transformed to a list of strings by using the map() function by passing the datatype str. The map function will return a map object, to convert the map object into a list the list() function is used. We have mapped the list of strings to a list of lists of strings by using the map function in the below example.

mylist = [3,9,12,16,4]

numbers = list(map(str, mylist))


print('map list str:',numbers)
 
#Python list map list of string to list of lists

data = ['Dev', 'Enum', 'Techn']

mapLst = list(map(list, data))


print('map list to list:',mapLst)

Output

map list str: ['3', '9', '12', '16', '4']
map list to list: [['D', 'e', 'v'], ['E', 'n', 'u', 'm'], ['T', 'e', 'c', 'h', 'n']]

6. Python map list to inputs


In this Python program, We are using the map(), input(), and split() Python function to take space-separated input as a list.

  • input: To display and message and take input User.
  • split(): Split the input string and return a list of strings.
  • map(): The map() is used to apply a function to each input element of the list.
  • List: to convert the input value into the list.
value = list(map(str,input("Enter mutiple String input :").split()))
print('You have entered :',value)

Output

Enter mutiple String input :Welcome to devenum 
You have entered : ['Welcome', 'to', 'devenum']

7. Python list map to two list


Sometimes we have to map two lists into one list, to achieve this we have used the map() function that will transform the elements of the list by applying the lambda function. It will return a map object that is converted to a list using the list() method.

data = ['Dev', 'Enum', 'Techn']

info = ['C#', 'Python' , 'Rust']

mapLst = list(map(lambda Item1, item2: Item1+ ' ' +item2, data, info))


print('map list to list:',mapLst)

Output

map list to list: ['Dev C#', 'Enum Python', 'Techn Rust']