Advertisement
gauravssnl

map_list_introduction

Sep 30th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # map_list_introduction.py
  2. #other ways of mapping also shown
  3. def double(n):
  4.     return n*2
  5. li1=[1,2,3,4,5,6]
  6. li2=['a','b','c','d','e']
  7. mapped_list1= map(double,li1)
  8. mapped_list2= map(double,li2)
  9. print mapped_list1
  10. print mapped_list2
  11. # one line map code
  12. ml1=map(lambda x: x*2,li1)
  13. ml2=map(lambda x: x*2,li1)
  14. print ml1
  15. print ml2
  16. # map by list comprehension
  17. nl1=[i*2 for i in li1]
  18. nl2=[i*2 for i in li2]
  19. print nl1
  20. print nl2
  21.  
  22. # map by for loop
  23. newlist1=[]
  24. newlist2=[]
  25. for i in li1:
  26.     newlist1.append(double(i))
  27. for i in li2:    
  28.     newlist2.append(double(i))
  29.  
  30. print newlist1
  31. print newlist2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement