Python List Comprehension for Data Science / Data Analysis - P.4
Key Takeaways
This video covers Python list comprehension for data science and data analysis, including list, set, tuple, and dictionary comprehension, with examples and code snippets in Python using tools like VS Code and Jupyter Notebook.
Full Transcript
what up data nerds i'm luke and welcome to my channel where i make data visualization easy in this video today we're gonna be going over list comprehension and that's just a fancy way of using for loops in a more concise manner to make lists and it's not just limited to lists we can do tuple sets and dictionaries as well but it's just a more concise way to write your code so for today we're going to be focusing on three parts if you will so the first part is uh focusing on just some list comprehension what is the layout of it from there we move into other comprehension methods how we can do with tuples sets and dictionaries and then finally we wrap it up with a third thing with using if statements within a list comprehension to make it just a little bit more complex at the end of this we're going to wrap it up all together and we're going to do a practice problem where we go into the office data set that has all the different scripts and lines of all the actors and what seasons they were in we're going to make it to where you can input an actor's name and get out what the seasons they were part of using set comprehension so with that let's jump right into the computer so here we are in my mac with vs code launched and a jupiter notebook running and the first one as we discussed will be that we're going to be talking about is list comprehension so to understand list comprehension let's have a quick recap of four loops on how you would do this with just a for loop and not using that comprehension so focus on this top line of code first you would need to create an empty list then from there you would use the for loop to iterate through an iterable and then we'd use in this case it's a list we'd use the append method to a list to do some sort of output expression so you could put that iter from the interval inside of here um yeah so you would do this method to actually create the list now moving on to actual list comprehension you can see this is much more concise code that we're accomplishing one line of code by three lines of code and for this very similar layout except now you don't have to create an empty list and you don't have to do this append method so what you have is you first list the output expression so what you're wanting to go into the list and then from there you get the you put the for loop and that's what you're cycling through so for it are in iterable so let's look at an example of a for loop and then for list comprehension so let's say for the for loop first uh we're going to do the same example we want to get a list of 0 to 100 how will we do that so for the for loop first we'd have to like we said we have to create a blank list then we'd have to create a for loop to cycle through we want to go through 100 so we'd use this range 100 function and then from there we'd want to append that to numbers so numbers dot append and we would want to append number to that list and then if we go and actually print this so numbers shift enter we can see hey we have this list of a hundred numbers so let's do this now with uh list comprehension so remember similar syntax we have our we want to start with our output expression so we want to start with number um and remember that that's going to be a self-defined variable within here and then we do the for loop so for number in range 100 and doing shift enter hey we get the same list of 100 numbers next let's get into modifying that output expression and we'll do it for four loops again and then we'll compare it to list comprehension and remember so we have an output expression here before we were just putting the iter inside of the output expression but i want to show that you can actually put different things in here so let's say we wanted the uh the numbers were appended but we want to have uh 0 through 100 but squared so we could do number and squared within our for loop and then whenever we do shift enter oh yeah right here and so then when we do shift enter we can see hey these are all the different values that we have for being squared and so let's do similar with the list comprehension so similar here right that we have our output expression on the beginning so i would just put number squared at the beginning still doing the four number in range 100 do shift enter and then yeah yeah here it is so it's uh all the values are now squared within this list now let's look at comprehension for other objects so we're not just limited to lists we can also do tuples sets and dictionaries and just remember recall that two poles why would you want to use tuples well tuples are immutable so if you wanted some sort of list if you will that you can't change you'd use that for sets this is more of unique values and unordered and that's why we're going to be using this in the final practice problem so make sure you take note of this and then dictionary would be used to have some sort of key and value pairs and so for the tuple and set comprehension very similar structure to the list comprehension except you're changing the outer characters so instead of brackets for lists tuple's going to be parentheses for set it's going to be these curly brackets and so on for a dictionary so for tuple comprehension let's say we wanted to get a list of values for whenever we go from 0 to 10 and we'd want to divide it by 2 and we want to get the remainder and we want to see what this would look like so for this similar as before right we would do we would do number and we want the remainder so we're using this modulus operator so number modulus 2 for number and we'll just do range 10. and we do shift enter so one thing to note about tuple comprehension compared to these other ones this is actually a special case and it's not necessarily included within python but all you have to do to convert this generator object into a tuple is put tuple outside of here and that's just using the built-in tuple function so pressing shift enter hey we get what we would expect to see we get the the remainders as two as we go through the number range divided by two what is the remainder uh zero one zero one moving on to set comprehension we're gonna be doing the same example here but with a set instead and like before we want to get the remainder of going from 0 to 10 and dividing it by 2 seeing what the remainder is and recall right from above the two bowls that it had a lot of repeat values in it and remember tuples are immutable sets however we can change them and the thing with sets that are great is they only provide unique values so in this we would get the values that didn't have any repeats so whenever i run this shift enter i'm getting only the unique values which if we look at the tuple above it's a bunch of zeros and ones for the set it's just zero and one now moving into dictionary comprehension so very similar structure and we're going to be using the same example as before we want to use the modulus operator to get the remainder and let's say we wanted to have as the key we wanted to have that number uh from that num in range 10 as the key and then for the value we would have what the remainder is and so the the special portion of this for dictionary comprehension is you put your output expression up here and then you have this colon in between it to signify that key and value and then you just as before you just cycle through some some for loop so running uh shift enter we can see and get the results and now we have a key to see what the value is associated with it so let's take the dictionary comprehension a step further and this is a problem i'll run into frequently uh with that analysis is i'll have two different lists and they're ordered in the same manner that they um coordinate with so in this case i have two lists here names and roles michael the first one is associated with the first role of regional manager pam's associated with receptionists and so on but i want to i want to make a dictionary out of it where i have names as the keys and roles as the values so how would i do that with dictionary comprehension so for this since i have two different lists i would use the built-in function from python zip and if i so if i run if i write this zip names and roles and press shift enter i get this zip object i want to see it and so i'll convert this into a list so i'll put this in parentheses shift enter and then now we can see it's a for this we convert it to a list but it's a list of uh tuples so you have your your michael and your regional manager pam receptionist and so on okay so now we understand what the contents are of this zip let's apply this to a dictionary comprehension case so this is what the dictionary comprehension would then look like we would do we want to have name as the key and role as the value and then from there we would do for name role in zip names roles and recall right this is this for loop portion right here is going to be going through we have we have two different objects that we're placing within it and that's because it's going to be cycling through each one of these tuples individually so the first one that it gets to is gonna be michael regional manager it'll assign michael to name regional manager to roll and then from there we can put it into the dictionary itself so let's actually run this shift enter bam we can see with the curly bracket braces now on the outside of it we have this uh the key uh as the names and then the value as the role so let's move into the third and final part for this and understanding list comprehension and that's using uh conditions within it specifically we're gonna look at if we include an if statement so for the list comprehension we're gonna have the same structure with an add-on so we still start with our output expression from there we can put in our for loop that we're going to iterate through and then we tack on an if statement so to evaluate a certain condition and that will filter out the list so let's we're going to first let's look at a for loop first okay so here's the for loop remember that we had before where we were generating a list from zero to a hundred but now let's say we only want those values that are even so whenever you divide it by 2 the remainder is 0. how would you go about doing that so for this what we'd have to do is one we'd cycle through all the numbers in the range and then from there we'd have to add a statement within it so if number and the modulus of two is equal to zero if we meet this condition i.e the number is even from there we'd want to append the number if it wasn't then we wouldn't append the number so actually running shift enter we can see hey we get the list of even numbers so now let's do the same thing but with list comprehension so remember we start with the output expression and then for number in range 100 because we go through the net and then it's going to be do an if condition so if number modulus 2 is equal to 0 i.e the number is even we want to insert it into the list pressing shift enter we get the list with just even numbers moving on to the application of what we just learned so for the application um as previous described we want to display the season the seasons and actors was a part of in the office based on user input and for this we have a data set already and what it is is is in the csv which i have loaded in excel but what it does is it puts in all the different lines that the speaker or actor has and then it from there it also has things like the season that this was part of the episode and also the scene for this we really just care about column c so the episode that it was part of and then column f the speaker so we need to one get the contents of this in there and then from there do the comprehension method to get the data that we want so looking at the approach that we're going to be taking for this we're going to first start with we want to read the contents of the csv from there recall that that is we're going to have in that csv file we have a bunch of repeat values within it because we have a bunch of names associated with seasons that are going to get repeated over and over again so for this we're going to want to use set comprehension so we're going to use set comprehension to develop this values that we want from there we're going to work into just fine-tuning it further we want it to where we can put have user input so we'll be using the input input function for people to put in different names and try out things to see what seasons they're a part of so let's start coding so let's do let's read the contents of the csv file first so for this we're going to need to import the csv module which is part of python standard library from there we want to open this file so we'll say hey csv file equal to open using the python built-in function open and i'll place the location of this file and just so you can see we have within our repository right here we have a folder for the office data and then the scripts of the office and that's what we're accessing to open and then as always good practice whenever you open a file you want to close it so we'll use the close method and we're just going to run this see if we have any errors and shift enter so no errors okay so now what we need to reopen the file we need to read the file so from here we'll say hey csv reader is equal to so use this csv model module dot reader and then from there we will put in the file so csv file and to see the contents of what's going on here we'll just do a csv reader print statement within there and do shift enter and yeah to reader object and just to show that we can iterate through this reader object so we could say hey for row in csv reader i want to print uh the row and then i want to break because i want to stop after the first four iteration and uh if you were following along for the last episode similar we're on the similar path here we have all of the different items for the from the first line within the csv so looking at the first line right here that's what we're getting right here so remember we had said previously hey we just want we went to the season and we want to know the speaker itself and just uh reiterate right so season one we can see just for michael alone michael is appearing multiple times with season one associated so that's why we're gonna be using set comprehension for this but to to just keep on building on this right we need to get the columns from this sheet itself so for this we need the season column and we need the speaker column and remember python is indexed by zero so we need zero one and then two three four five so we need to rows one and five so just printing this to the printing this below to actually see that we're getting the right things we're gonna put row one and row five and yep we get season and speaker okay so now let's go about building this uh set comprehension to get this value for the time being i don't need this for loop but i just want to keep it just in case so i'm going to comment it out if i press shift alt and a i can comment multiple lines so let's get into building this set comprehension and just as a model to use a we're gonna have this is what the model i would expect the set comprehension to have right you have your output expression for statement and then your if condition uh so we're gonna go through now and build a set comprehension based on this model so i start with our curly brackets and then remember we want for the output expression remember we have row one and row five we won the season in this set comprehension so we're going to use row 1 and then we're going to do for row in csv reader so similar to what we did above to cycle through and we want to do it if remember the user is going to input an actor name so we're going to say if row 5 which is the actor name is equal to actor name and so actor name hasn't been defined yet and i did the wrong equal sign remember when you do evaluates so i need to define an actor name and i'll just define that up above here so we'll say actor name and for the time being we'll just give it to uh to michael okay so just uh to recap right we've uh we're pulling out the seasons with us so row one we cycle through each of the rows in the csv reader and then as we're doing that we're looking at that row five column to see what the name is and seeing that it makes sure that it matches the actor name that we want and we're using a set so it's not going to have any duplicates and i need to actually output it so um i want to do print i want to print this we do a shift enter we can see hey this is uh the set uh for the seasons that michael was a part of and so for this we see this is the seasons that michael was a part of and it's not organized but it has all the different seasons no duplicates and you can see hey season eight he wasn't part of season eight so instead of just hard coding this name in here let's change this to the built-in function input and then from there have a statement for users to input the the name they want to use okay so now we have that in here and then uh let's make this a little bit prettier so this will be seasons equal to this and we'll send it to the variable and then from there i want to print something so i wanted to print one i wanted to print the actor's name that we input and what seasons they're a part of okay so now we have it in here we have just a recap right we have it to where the user can input the name from there we read inside of the file and then we use list comprehension to comprehend this and i'm going to go ahead i don't need this this anymore and i don't need this comment block so i'm going to just simplify my code and yeah actor's name and then from there it prints the actor name that they input appeared in seasons and then it prints out the seasons as a set so let's do let's evaluate this we'll do shift enter oh and yep so i need to have a comma right here and we'll do shift enter okay and it says hey name an actor to see what seasons they're a part of and fun fact warren buffett was actually on the show so we can do warren buffett and we see hey warren buffett appeared in seasons seven and then yeah just to double check that all the data is there we can do shift enter again and let's just say we wanted to see something like uh gym so we'll do gym shift enter and see hey jim appeared in these seasons so one two three four five six seven eight nine bam so there you have it that is a list comprehension and tuple and dictionary comprehension in a nutshell just to reiterate if you're new to python feel free to keep on using the four statements but i would slowly try to integrate this list comprehension to make your code more concise and therefore you're less prone to have errors for the next episode we're going to be going into functions so understanding what functions are how they are used and then from there automating some of the tasks of extracting the data from the office data set so as always if you found this video useful smash that like button if you're following along in this video series consider subscribing and then finally comment down below on things that you may have questions on with list comprehension or ways that you're using list comprehension within your data analysis projects so with that i'll see you in the next episode
Original Description
Python Fundamentals Course (DataCamp) 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Data Analyst Track w/ Python (DataCamp) 👉🏼 https://lukeb.co/PythonAnalystDataCamp
(My recommended courses that I took to learn Python!)
This video provides an overview of List, Set, Tuple, & Dictionary Comprehension frequently used by Data Analysts and Data Scientists. We cover how to build multiple examples for data analysis purposes including conditional statements (i.e. if statements). Finally, we combine what we learned to use set comprehension to provide a list of seasons that an inputted actor is a part of for the TV show 'The Office.'
Playlist (Course) Material 👉🏼 https://github.com/lukebarousse/Python_Intro
00:00 Intro
01:10 List Comprehension: Comparing For Loops
03:41 List Comprehension: Modifying Output Expression
04:47 Other Comprehension Methods
05:32 Tuple Comprehension
06:41 Set Comprehension
07:28 Dictionary Comprehension
08:16 Dictionary Comprehension w/ zip()
10:45 Conditions in Comprehension: If Statements
12:38 Application: Season Search Tool
14:17 Application: Import Data from CSV
17:13 Application: Set Comprehension
19:08 Application: Building User Input
21:34 Conclusion/Next Video
Basics of Python playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT8zTx9XO9jFf4PQ0iwBWfXB
Setting up Python on your computer playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT9udgCeqZpS4HKF6uIzra3r
Recommended Certificates/Courses (Affiliate Links)
==================================
DataCamp:
Python 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Power BI 👉🏼 https://lukeb.co/PowerBIDataCamp
Tableau 👉🏼 https://lukeb.co/TableauDataCamp
R 👉🏼 https://lukeb.co/RDataCamp
Data Analyst w/ Python 👉🏼 https://lukeb.co/PythonAnalystDataCamp
DataCamp Subscription (Monthly $25USD) 👉🏼 https://lukeb.co/DataCampSub
Coursera:
Google Data Analytics Certificate (BEGINNERS START HERE) 👉🏼 https://lukeb.co/GoogleCert
Python for Everybody 👉🏼 https://lukeb.co/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Luke Barousse · Luke Barousse · 20 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
▶
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Connect Google Sheets to Tableau & Joining Data - Tableau Tutorial P.1
Luke Barousse
How To Use Tableau Desktop Controls - Tableau Tutorial P.2
Luke Barousse
Dimensions Vs Measures (Blue Vs Green Data) - Tableau Tutorial P.3
Luke Barousse
Create Stacked Bar Chart (and any other visuals EASILY!) w/ Show Me! - Tableau Tutorial P.4
Luke Barousse
Conditional Format Tables in Tableau (Like Excel!) - Tableau Tutorial P.5
Luke Barousse
Calculated Fields in Tableau (Formulas & IF Statements) - Tableau Tutorial P.6
Luke Barousse
Parameters (Create & Use in Calculated Fields and/or Visuals) - Tableau Tutorial P.7
Luke Barousse
Totals, Average Lines, & Trend Lines (Analytics Pane) - Tableau Tutorial P.8
Luke Barousse
How To Create a Dashboard - Tableau Tutorial P.9
Luke Barousse
Upload your dashboard to Tableau Public - Tableau Tutorial P.10
Luke Barousse
Install Python for Data Science on Mac & Windows (PC) with Anaconda - P.1
Luke Barousse
How to run Python for Data Science - Editors vs IDEs - P.2
Luke Barousse
Install VS Code with Python for Data Science / Data Analysis - P.3
Luke Barousse
Understanding Virtual Environments for Data Science / Data Analysis - P.4
Luke Barousse
Using VS Code with Python for Data Science / Data Analysis - P.5
Luke Barousse
Python for Data Science / Analysis ft. 'The Office' Dataset - P.0
Luke Barousse
Python Objects frequently used in Data Science / Data Analysis - P.1
Luke Barousse
Python If Statements for Data Science / Data Analysis - P.2
Luke Barousse
Python For & While Loops for Data Science / Data Analysis - P.3
Luke Barousse
Python List Comprehension for Data Science / Data Analysis - P.4
Luke Barousse
Python Functions for Data Science / Data Analysis - P.5
Luke Barousse
Lambda Functions for Data Science / Data Analysis - Python P.6
Luke Barousse
How NOT to learn Python for Data Science
Luke Barousse
What is Business Intelligence (BI)? 📊😅
Luke Barousse
Top 3️⃣ Technical Skills for Business Intelligence 📚📊
Luke Barousse
Top Non-technical Skills for Business Intelligence 📊👨🏼💻
Luke Barousse
M1 vs Intel Mac for Data Science
Luke Barousse
M1 vs Intel Mac for Excel 📈👨🏼💻
Luke Barousse
M1 vs Intel Mac for Python 🐍👨🏼💻
Luke Barousse
M1 vs Intel Mac for Business Intelligence Tools 💻📊
Luke Barousse
M1 Macbook Air vs Pro (8 vs 16 GB) for Data Science
Luke Barousse
Python for M1 Mac vs Intel (SPOILER: M1 is 2x faster)
Luke Barousse
Data Analyst's WFH Setup & Upgrades
Luke Barousse
Windows on the M1 Mac - What are your options?
Luke Barousse
Install your favorite Windows app on M1 Mac - ft. Parallels
Luke Barousse
Data Science shortcuts for Mac
Luke Barousse
Day in the life of a data analyst
Luke Barousse
Power BI vs Tableau - Best BI Tool
Luke Barousse
Mac Vs PC - BEST for Data Science
Luke Barousse
Data Scientist vs Data Analyst (funny!)
Luke Barousse
Become a DATA ANALYST with NO degree?!? The Google Data Analytics Professional Certificate
Luke Barousse
Certificates vs Degree for Data Analysts (ft. Google Data Analytics Professional Certificate)
Luke Barousse
Google vs IBM Data Analyst Certificate - BEST Certificate for Data Analysts
Luke Barousse
Python Vs R (funny!)
Luke Barousse
THIS got me my job as a Data Analyst - My portfolio tip
Luke Barousse
I used Python to Count my Bike Jumps!
Luke Barousse
Standout as a Data Analyst with THIS TOOL
Luke Barousse
STOP using Spreadsheets for Everything!
Luke Barousse
Transition into Data Science - My Tips & Story
Luke Barousse
Get a JOB w/ Google Data Analytics Certificate?!? (ft. Certificate Holders)
Luke Barousse
Staying Motivated in Data Science
Luke Barousse
Data Science - Expectation vs Reality (funny!) - ft. @KenJee_ds
Luke Barousse
Get NOTICED in Data Science!!! (3 types of GREAT projects)
Luke Barousse
Use THIS to showcase EXPERIENCE in Data Science
Luke Barousse
How to show EXPERIENCE... when you have NONE?!?
Luke Barousse
Learn PYTHON to be a DATA ANALYST?!? (or is R enough...)
Luke Barousse
The BIGGEST MISTAKE when starting a data project!
Luke Barousse
Top Jobs in Data Science
Luke Barousse
How to get Data Analytics side jobs - NEW LinkedIn Feature
Luke Barousse
Building a bot to scrape job data… How NOT to collect data
Luke Barousse
More on: ML Maths Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Python for Data Science — Probability Basics for Data Science
Medium · Data Science
Python for Data Science — Probability Basics for Data Science
Medium · Python
The Attention Economy: Your Attention Is Worth More Than Gold
Medium · Data Science
What I Learned Building a Tableau Dashboard for Deloitte’s Data Analytics Simulation
Medium · Data Science
Chapters (14)
Intro
1:10
List Comprehension: Comparing For Loops
3:41
List Comprehension: Modifying Output Expression
4:47
Other Comprehension Methods
5:32
Tuple Comprehension
6:41
Set Comprehension
7:28
Dictionary Comprehension
8:16
Dictionary Comprehension w/ zip()
10:45
Conditions in Comprehension: If Statements
12:38
Application: Season Search Tool
14:17
Application: Import Data from CSV
17:13
Application: Set Comprehension
19:08
Application: Building User Input
21:34
Conclusion/Next Video
🎓
Tutor Explanation
DeepCamp AI