Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 6, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/d4a5b1873b5a5b41a6810e88c71e1dfe to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/d4a5b1873b5a5b41a6810e88c71e1dfe to your computer and use it in GitHub Desktop.
import itertools
l1=itertools.starmap(pow,[(0,2),(1,2),(2,2)])
print (l1)#Output:<itertools.starmap object at 0x00C8E4D8>
#We can iterate through starmap object,using for loop or using next() function. We can also convert to list object.
print (list(l1))#Output:[0, 1, 4]
#using map()
l2=map(pow,[0,1,2],[2,2,2])
print (list(l2))#Output:[0, 1, 4]
a1=map(lambda x:x**2,[1,2,3])
print (list(a1))#Output:[1, 4, 9]
#If elements inside the iterable are not iterable, it will raise TypeError.
a2=itertools.starmap(lambda x:x**2,[1,2,3])
print (list(a2))#Output:TypeError: 'int' object is not iterable
a3=itertools.starmap(lambda x,y:x+y,[(0,1),(1,2),(2,3)])
print (list(a3))#Output:[1, 3, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment