List Comprehensions¶
Let us see how we can process data in Python collections using list comprehensions.
- Let us create list of tuples using id and email from the below employees using list comprehensions.
- We will ensure that employee id is of type
int
. - Here is the approach with conventional loops.
- Create a global list.
- Process each string and extract id and email. Type cast id to
int
. - Append to the list.
- By the time
employees
is processed, we will have a new list which contain tuples with id and email.
In [1]:
employees = [
'1,ktrett0@independent.co.uk,6998.95',
'2,khaddock1@deviantart.com,10572.4',
'3,ecraft2@dell.com,3967.35',
'4,drussam3@t-online.de,17672.44',
'5,graigatt4@github.io,11660.67',
'6,bjaxon5@salon.com,18614.93',
'7,araulston6@list-manage.com,11550.75',
'8,mcobb7@mozilla.com,17016.15',
'9,grobardley8@unesco.org,14141.25',
'10,bbuye9@vkontakte.ru,12193.2'
]
In [2]:
employees_emails = []
for employee in employees:
employees_emails.append((int(employee.split(',')[0]), employee.split(',')[1]))
In [3]:
employees_emails
Out[3]:
[(1, 'ktrett0@independent.co.uk'), (2, 'khaddock1@deviantart.com'), (3, 'ecraft2@dell.com'), (4, 'drussam3@t-online.de'), (5, 'graigatt4@github.io'), (6, 'bjaxon5@salon.com'), (7, 'araulston6@list-manage.com'), (8, 'mcobb7@mozilla.com'), (9, 'grobardley8@unesco.org'), (10, 'bbuye9@vkontakte.ru')]
- Here is the approach using
list
comprehensions.- We can directly write the code using
for
loop and assign it to a variable. - The variable will be of type
list
. - There is no need to initialize a list and use
append
function.
- We can directly write the code using
In [4]:
employees_emails = [(int(employee.split(',')[0]), employee.split(',')[1]) for employee in employees]
In [5]:
employees_emails
Out[5]:
[(1, 'ktrett0@independent.co.uk'), (2, 'khaddock1@deviantart.com'), (3, 'ecraft2@dell.com'), (4, 'drussam3@t-online.de'), (5, 'graigatt4@github.io'), (6, 'bjaxon5@salon.com'), (7, 'araulston6@list-manage.com'), (8, 'mcobb7@mozilla.com'), (9, 'grobardley8@unesco.org'), (10, 'bbuye9@vkontakte.ru')]
- We can also apply conditions as part of list comprehensions. The below logic will create list of employees whose salary is greater than $1000.
In [6]:
highest_salaried_employees = [employee for employee in employees if float(employee.split(',')[2]) > 10000]
In [7]:
highest_salaried_employees
Out[7]:
['2,khaddock1@deviantart.com,10572.4', '4,drussam3@t-online.de,17672.44', '5,graigatt4@github.io,11660.67', '6,bjaxon5@salon.com,18614.93', '7,araulston6@list-manage.com,11550.75', '8,mcobb7@mozilla.com,17016.15', '9,grobardley8@unesco.org,14141.25', '10,bbuye9@vkontakte.ru,12193.2']
In [8]:
highest_salaried_employees = [
(int(employee.split(',')[0]), employee.split(',')[1])
for employee in employees if float(employee.split(',')[2]) > 10000
]
In [9]:
highest_salaried_employees
Out[9]:
[(2, 'khaddock1@deviantart.com'), (4, 'drussam3@t-online.de'), (5, 'graigatt4@github.io'), (6, 'bjaxon5@salon.com'), (7, 'araulston6@list-manage.com'), (8, 'mcobb7@mozilla.com'), (9, 'grobardley8@unesco.org'), (10, 'bbuye9@vkontakte.ru')]
- We can also perform aggregations using comprehensions.
In [10]:
total_salary = round(sum(float(employee.split(',')[2]) for employee in employees), 2)
In [11]:
total_salary
Out[11]:
124388.09
In [12]:
min(float(employee.split(',')[2]) for employee in employees)
Out[12]:
3967.35