How To Use Sets in Python (Python Tutorial #13)
Skills:
Python for Data90%
Key Takeaways
Uses Python sets for data manipulation and storage
Full Transcript
hi everyone in this video I'm going to show you how to use sets in Python now what is a set well in Python a set is a type of data that stores a set of things and this is actually a set of unique things so for example if you have a set of these two numbers 1 & 3 and if you try to add another number 3 here to this set nothing happens and you still have this set 1 & 3 because a Python set just rejects any duplicate elements ok let's take a look at some code examples here if you write a eCos set parenthesis this creates a new empty set and then it assigns it to this variable a so if you print a by writing print a and then once you run the cell this thing is printed set parenthesis this just represents an empty set all right and to add elements to this set for example a number you can just write a dot add parentheses 1 and this as the number 1 to this set so once you print a you see this thing curly brackets and then 1 inside this just represents a set with only one inside the set and after that if you write a dot add 2 you'll add 2 to this set and I want to print a again you see that now this is a set of two numbers 1 and 2 and then if you try to add the number 2 again by writing it I add 2 nothing happens and you still get the same set because again a Python set rejects any duplicates ok one useful thing to do with a set is to iterate over every element in the set so you can do that with 4 X in a and then print X so you see that this is exactly the same as how we iterate over each element in a list so if a was a list you know this part will look exactly the same and this way we can iterate over every element in the given set a so let's see if you works by running the cell and it does we see 1 & 2 being printed ok so when should you use a set one example is when you want to remove duplicates from let's say a given list so let's say you're given this list called given list one with these elements 1 1 2 4 & 2 here if you want to remove the duplicates you know these two ones and these two twos so that you only have 1 2 & 4 and let's say a new list you can use a set here to do that you can just write this first create a new set by writing new set let's say 1 equals set parentheses this creates an empty set and then after that you can go over each element in the given list by writing for X in given list 1 : 4 spaces you set 1 dot add X so this way we're saying we want to add each of these elements 1 1 2 4 & 2 to this new set use that one and after that you just need to print new set 1 and what do you think we're gonna get we'll get 1 2 & 4 because we're adding every element in the given list but this set automatically rejects any duplicates and that's why we're left with these unique elements okay and what if you want to create a new list that only contains these unique elements from the original list think about it for a second and here's my solution first create a new list by writing new lists 1 equals square brackets this creates a new empty list you can do the same thing by writing lists parentheses as well and then go over every element in the set in this set 1 2 & 4 by writing for X and you set 1 : 4 spaces new lists 1 the append so this way we're going over every element in the new set one and then we're appending that to new list one by writing new list one dot append X so after that once you print new list one you should see this list one two and four and just a quick note here you can add things to a set there are not numbers so for example you can do this B equals set this creates an empty set and assigns it to be and then you can write beta add single quotes Apple and then be the add single quotes banana and this way we're adding the strings apple and banana to this set and you can even mix multiple types of things in a single set so you can do add one and then this as apple banana which are strings and one which is a number two the same set B so once you print B you should see this one banana and apple okay and I think one thing to note here is that you know the order in which you see these elements one banana and Apple is different from the order in which we added these things apple banana and then one so a set is a type of data that doesn't store the order in which things have been added to the set and that's one way in which a set is different from a list in Python and that's because when you add things to a list by using the append function like we saw earlier here the order in which these things have been added to a list is preserved okay let me give you a simple exercise problem to practice using what you just learned so let's say you're given this list given a list - and with these elements one three four one and three the problem is can you find the sum of unique elements in this list so the unique elements are obviously 1 3 & 4 and the sum of those elements is 8 so your solution should be able to find this number eight and your solution should work no matter how many elements are in the given lists so think about it for a second and I'm gonna give you my solution in a second okay here's my solution first create a new set by writing new underscore set to eCos set parenthesis and then run a for loop over given list two by writing for X in given list two : new set to dot add X so this way we're adding all the unique elements to this set so after this for loop this is gonna have the elements one three and four and to find the sum of those elements there's actually a shortcut for it in Python you can just write some new set too but here let's not use that for now and let's do it in a more explicit way so we'll first create a new variable called the total and then let's set it to zero and then we'll just add every element in new set to two total we can do that by writing for X and you set two : total plus equals x so this way we're adding each element in new set to which were calling X two total and once you print total you should get eight let's see if it works and it does okay that's it for this video thank you as always for watching my videos and I'll see you guys in the next one
Original Description
Python tutorial - how to use sets!
This entire series in a playlist: https://goo.gl/eVauVX
Also, find me on Instagram: https://www.instagram.com/ykdojo/
And Twitter: https://twitter.com/ykdojo
And Facebook: https://www.facebook.com/entercsdojo
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 55 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
▶
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI