Reputation: 45
def ups(*name):
for n in name:
a=n.upper()
return a
lis=["lan","ona"]
m=list(map(ups,lis))
print(m)
Here in the map I have not done unpacking of the list, but the same in case of function call for without Map(), (eg) like ups(*lis)
is must, why is that?
Learning, Thanks
Upvotes: 2
Views: 198
Reputation: 25634
In addition to ksourav's answer,
map(function, iterable, ...)
"Return[s] an iterator that applies function to
every item of iterable, yielding the results". As ksourav points out
in his answer, the items you pass are strings and thus iterables
themselves - so the function just returns the last letter in
uppercase, likes = 'lan'
for char in s:
print(char.upper())
# L
# A
# N
tuple
and not the individual elements of
the string anymore. This is why here, your function returns
the whole word in uppercase letters, liket = ('lan',)
for element in t:
print(element.upper())
# LAN
m = list(map(lambda x: x.upper(), lis))
# or even better
m = [s.upper() for s in lis]
Upvotes: 2
Reputation: 124
star()args means variable number of arguments.so basically your fuction can take variable number of arguments.On the other hand map function takes a function and a list as argument and the function is called on each element of the list that was passed as argument.so If you don't use "" in the function which you have defined, then it will take one sting in the lis(as you are passing in to the map function) and iterate over it and return the only last letter of each string in uppercase.Because for loop will iterate over the element passed but return only the last element as you are returning only a.
Upvotes: 0