Usage of Lambda Functions¶
Lambda functions are typically used while invoking those functions which take functions as arguments. Here are some of the functions which take other functions as arguments.
filter
map
sorted
orsort
and more
There are many Python modules which have functions which take other functions as arguments. We will see quite a lot of examples in upcoming chapters.
In [1]:
filter?
Init signature: filter(self, /, *args, **kwargs) Docstring: filter(function or None, iterable) --> filter object Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true. Type: type Subclasses:
In [2]:
map?
Init signature: map(self, /, *args, **kwargs) Docstring: map(func, *iterables) --> map object Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted. Type: type Subclasses:
In [3]:
sorted?
Signature: sorted(iterable, /, *, key=None, reverse=False) Docstring: Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order. Type: builtin_function_or_method
In [4]:
users = [
'1,Bryn,Eaken,beaken0@cbc.ca,Female,236.34.175.186',
'2,Smith,Combe,scombe1@xing.com,Male,',
'3,Roland,Wallentin,rwallentin2@aboutads.info,Male,94.67.195.44',
'4,Charlotta,Richten,crichten3@wikia.com,Female,202.51.201.1',
'5,Berne,Coyne,bcoyne4@squarespace.com,,150.246.120.53',
'6,Kesley,Hakonsen,khakonsen5@cbc.ca,Female,227.61.48.174',
"7,Gray,M'Barron,gmbarron6@altervista.org,Male,190.128.200.183",
'8,Melinde,Scarf,mscarf7@diigo.com,Female,134.41.141.5',
'9,Arturo,Warkup,awarkup8@nifty.com,Male,',
'10,Annette,Lowthorpe,alowthorpe9@marketwatch.com,Female,63.157.95.191',
'11,Melinda,McOwen,mmcowena@unblog.fr,Female,73.95.183.60',
'12,Minnie,Andrivel,mandrivelb@storify.com,,131.194.233.209',
'13,Taryn,Medhurst,tmedhurstc@ebay.co.uk,Female,65.191.102.59',
'14,Fanni,Whitley,fwhitleyd@who.int,Female,184.210.235.118',
'15,Jareb,Thunderchief,jthunderchiefe@arizona.edu,Male,150.159.27.112',
'16,Sharona,Haffard,shaffardf@kickstarter.com,Female,91.125.183.157',
'17,Pattin,Basant,pbasantg@gov.uk,Male,162.227.228.164',
'18,Beatrice,Butler,bbutlerh@abc.net.au,Female,139.49.169.236',
'19,Linoel,Bucktrout,lbucktrouti@google.ru,Male,18.17.136.105',
'20,Katee,Aveyard,kaveyardj@feedburner.com,Female,60.25.33.89'
]
In [5]:
type(users)
Out[5]:
list
In [6]:
type(users[0])
Out[6]:
str
In [7]:
males = filter(
lambda user: user.split(',')[4] == 'Male',
users
)
list(males)
Out[7]:
['2,Smith,Combe,scombe1@xing.com,Male,', '3,Roland,Wallentin,rwallentin2@aboutads.info,Male,94.67.195.44', "7,Gray,M'Barron,gmbarron6@altervista.org,Male,190.128.200.183", '9,Arturo,Warkup,awarkup8@nifty.com,Male,', '15,Jareb,Thunderchief,jthunderchiefe@arizona.edu,Male,150.159.27.112', '17,Pattin,Basant,pbasantg@gov.uk,Male,162.227.228.164', '19,Linoel,Bucktrout,lbucktrouti@google.ru,Male,18.17.136.105']
In [8]:
male_and_females = filter(
lambda user: user.split(',')[4] in ('Male', 'Female'),
users
)
list(male_and_females)
Out[8]:
['1,Bryn,Eaken,beaken0@cbc.ca,Female,236.34.175.186', '2,Smith,Combe,scombe1@xing.com,Male,', '3,Roland,Wallentin,rwallentin2@aboutads.info,Male,94.67.195.44', '4,Charlotta,Richten,crichten3@wikia.com,Female,202.51.201.1', '6,Kesley,Hakonsen,khakonsen5@cbc.ca,Female,227.61.48.174', "7,Gray,M'Barron,gmbarron6@altervista.org,Male,190.128.200.183", '8,Melinde,Scarf,mscarf7@diigo.com,Female,134.41.141.5', '9,Arturo,Warkup,awarkup8@nifty.com,Male,', '10,Annette,Lowthorpe,alowthorpe9@marketwatch.com,Female,63.157.95.191', '11,Melinda,McOwen,mmcowena@unblog.fr,Female,73.95.183.60', '13,Taryn,Medhurst,tmedhurstc@ebay.co.uk,Female,65.191.102.59', '14,Fanni,Whitley,fwhitleyd@who.int,Female,184.210.235.118', '15,Jareb,Thunderchief,jthunderchiefe@arizona.edu,Male,150.159.27.112', '16,Sharona,Haffard,shaffardf@kickstarter.com,Female,91.125.183.157', '17,Pattin,Basant,pbasantg@gov.uk,Male,162.227.228.164', '18,Beatrice,Butler,bbutlerh@abc.net.au,Female,139.49.169.236', '19,Linoel,Bucktrout,lbucktrouti@google.ru,Male,18.17.136.105', '20,Katee,Aveyard,kaveyardj@feedburner.com,Female,60.25.33.89']
In [9]:
male_and_females = filter(
lambda user: user.split(',')[4] in ('Male', 'Female'),
users
)
len(list(male_and_females))
Out[9]:
18
In [10]:
users
Out[10]:
['1,Bryn,Eaken,beaken0@cbc.ca,Female,236.34.175.186', '2,Smith,Combe,scombe1@xing.com,Male,', '3,Roland,Wallentin,rwallentin2@aboutads.info,Male,94.67.195.44', '4,Charlotta,Richten,crichten3@wikia.com,Female,202.51.201.1', '5,Berne,Coyne,bcoyne4@squarespace.com,,150.246.120.53', '6,Kesley,Hakonsen,khakonsen5@cbc.ca,Female,227.61.48.174', "7,Gray,M'Barron,gmbarron6@altervista.org,Male,190.128.200.183", '8,Melinde,Scarf,mscarf7@diigo.com,Female,134.41.141.5', '9,Arturo,Warkup,awarkup8@nifty.com,Male,', '10,Annette,Lowthorpe,alowthorpe9@marketwatch.com,Female,63.157.95.191', '11,Melinda,McOwen,mmcowena@unblog.fr,Female,73.95.183.60', '12,Minnie,Andrivel,mandrivelb@storify.com,,131.194.233.209', '13,Taryn,Medhurst,tmedhurstc@ebay.co.uk,Female,65.191.102.59', '14,Fanni,Whitley,fwhitleyd@who.int,Female,184.210.235.118', '15,Jareb,Thunderchief,jthunderchiefe@arizona.edu,Male,150.159.27.112', '16,Sharona,Haffard,shaffardf@kickstarter.com,Female,91.125.183.157', '17,Pattin,Basant,pbasantg@gov.uk,Male,162.227.228.164', '18,Beatrice,Butler,bbutlerh@abc.net.au,Female,139.49.169.236', '19,Linoel,Bucktrout,lbucktrouti@google.ru,Male,18.17.136.105', '20,Katee,Aveyard,kaveyardj@feedburner.com,Female,60.25.33.89']
In [11]:
user_ips = map(
lambda user: (int(user.split(',')[0]), user.split(',')[-1]),
users
)
list(user_ips)
Out[11]:
[(1, '236.34.175.186'), (2, ''), (3, '94.67.195.44'), (4, '202.51.201.1'), (5, '150.246.120.53'), (6, '227.61.48.174'), (7, '190.128.200.183'), (8, '134.41.141.5'), (9, ''), (10, '63.157.95.191'), (11, '73.95.183.60'), (12, '131.194.233.209'), (13, '65.191.102.59'), (14, '184.210.235.118'), (15, '150.159.27.112'), (16, '91.125.183.157'), (17, '162.227.228.164'), (18, '139.49.169.236'), (19, '18.17.136.105'), (20, '60.25.33.89')]
In [12]:
sort_by_gender_and_id = sorted(
users,
key=lambda user: (user.split(',')[4], int(user.split(',')[0]))
)
sort_by_gender_and_id
Out[12]:
['5,Berne,Coyne,bcoyne4@squarespace.com,,150.246.120.53', '12,Minnie,Andrivel,mandrivelb@storify.com,,131.194.233.209', '1,Bryn,Eaken,beaken0@cbc.ca,Female,236.34.175.186', '4,Charlotta,Richten,crichten3@wikia.com,Female,202.51.201.1', '6,Kesley,Hakonsen,khakonsen5@cbc.ca,Female,227.61.48.174', '8,Melinde,Scarf,mscarf7@diigo.com,Female,134.41.141.5', '10,Annette,Lowthorpe,alowthorpe9@marketwatch.com,Female,63.157.95.191', '11,Melinda,McOwen,mmcowena@unblog.fr,Female,73.95.183.60', '13,Taryn,Medhurst,tmedhurstc@ebay.co.uk,Female,65.191.102.59', '14,Fanni,Whitley,fwhitleyd@who.int,Female,184.210.235.118', '16,Sharona,Haffard,shaffardf@kickstarter.com,Female,91.125.183.157', '18,Beatrice,Butler,bbutlerh@abc.net.au,Female,139.49.169.236', '20,Katee,Aveyard,kaveyardj@feedburner.com,Female,60.25.33.89', '2,Smith,Combe,scombe1@xing.com,Male,', '3,Roland,Wallentin,rwallentin2@aboutads.info,Male,94.67.195.44', "7,Gray,M'Barron,gmbarron6@altervista.org,Male,190.128.200.183", '9,Arturo,Warkup,awarkup8@nifty.com,Male,', '15,Jareb,Thunderchief,jthunderchiefe@arizona.edu,Male,150.159.27.112', '17,Pattin,Basant,pbasantg@gov.uk,Male,162.227.228.164', '19,Linoel,Bucktrout,lbucktrouti@google.ru,Male,18.17.136.105']