How To Use Sets in Python (Python Tutorial #13)

CS Dojo · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
2 Dynamic Programming Tutorial with Fibonacci Sequence
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
3 Kadane's Algorithm to Maximum Sum Subarray Problem
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
4 Longest Common Subsequence (Dynamic Programming)
Longest Common Subsequence (Dynamic Programming)
CS Dojo
5 0-1 Knapsack Problem (Dynamic Programming)
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
6 Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
7 Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
8 Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
9 Radix Sort Algorithm Introduction in 5 Minutes
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
10 Coding Interview Question and Answer: Longest Consecutive Characters
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
11 Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
12 Coding Interview Question: Tower Hopper Problem
Coding Interview Question: Tower Hopper Problem
CS Dojo
13 Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
14 Google Coding Interview Question and Answer #1: First Recurring Character
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
15 Facebook Coding Interview Question and Answer #1: All Subsets of a Set
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
16 Think you're not smart enough to work at Google? Well, think again.
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
17 How to Crack a Google Coding Interview - An Ex-Googler’s Guide
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
18 Amazon Coding Interview Question - K Closest Points to the Origin
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
19 How I Got an Internship at Microsoft
How I Got an Internship at Microsoft
CS Dojo
20 How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
21 Why I Left My $100,000+ Job at Google
Why I Left My $100,000+ Job at Google
CS Dojo
22 Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
23 How I Learned to Code - and Got a Job at Google!
How I Learned to Code - and Got a Job at Google!
CS Dojo
24 Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
25 What Is Dynamic Programming and How To Use It
What Is Dynamic Programming and How To Use It
CS Dojo
26 Python Tutorial for Absolute Beginners #1 - What Are Variables?
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
27 What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
28 How to Use If Else Statements in Python (Python Tutorial #2)
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
29 Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
30 How To Use Functions In Python (Python Tutorial #3)
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
31 What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
32 Introduction To Lists In Python (Python Tutorial #4)
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
33 Introduction to For Loops in Python (Python Tutorial #5)
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
34 What Programming Language Should I Learn First?
What Programming Language Should I Learn First?
CS Dojo
35 What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
36 While Loops and The Break Statement in Python (Python Tutorial #6)
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
37 More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
38 How to Learn to Code - Best Resources, How to Choose a Project, and more!
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
39 How To Use Dictionaries In Python (Python Tutorial #8)
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
40 Data Structures & Algorithms #1 - What Are Data Structures?
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
41 An Overview of Arrays and Memory (Data Structures & Algorithms #2)
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
42 Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
43 Classes and Objects with Python - Part 1 (Python Tutorial #9)
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
44 Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
45 Classes and Objects with Python - Part 2 (Python Tutorial #10)
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
46 Introduction to Linked Lists (Data Structures & Algorithms #5)
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
47 Introduction to Recursion (Data Structures & Algorithms #6)
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
48 Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
49 Amazon Coding Interview Question - Recursive Staircase Problem
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
50 Using Boolean in Python (Python Tutorial #11)
Using Boolean in Python (Python Tutorial #11)
CS Dojo
51 Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
52 What Can You Do with Python? - The 3 Main Applications
What Can You Do with Python? - The 3 Main Applications
CS Dojo
53 Facebook Coding Interview Question - How Many Ways to Decode This Message?
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
54 List Comprehension Basics with Python (Python Tutorial #12)
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
56 Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
57 Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
58 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
59 Google Coding Interview - Universal Value Tree Problem
Google Coding Interview - Universal Value Tree Problem
CS Dojo
60 Best laptops for programming? How to get a job at Google? - And other FAQ’s!
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo

Related Reads

Up next
How to Clean Up Your Backlog with AI & MCP | Businessmap Quick Tips
Businessmap
Watch →