🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are particularly useful for membership testing, removing duplicates from a sequence, and performing mathematical set operations like union, intersection, and difference.
Table of Contents
- Introduction
set()Function Syntax- Understanding
set() - Examples
- Basic Usage
- Removing Duplicates from a List
- Performing Set Operations
- Iterating Over a Set
- Real-World Use Case
- Conclusion
Introduction
The set() function creates a set, which is a collection of unique and unordered elements. Sets do not allow duplicate elements and support operations that are similar to mathematical sets. They are mutable, meaning you can add or remove elements after creation.
set() Function Syntax
The syntax for the set() function is as follows:
set([iterable])
Parameters:
- iterable (optional): An iterable (such as a list, tuple, or string) whose elements are to be added to the set. If no iterable is provided, an empty set is created.
Returns:
- A set containing the unique elements from the iterable.
Understanding set()
The set() function can take an iterable as an argument and return a set containing the unique elements of the iterable. If no argument is provided, it returns an empty set.
Examples
Basic Usage
To demonstrate the basic usage of set(), we will create a set from a list of numbers.
Example
# Creating a set from a list of numbers
numbers = [1, 2, 3, 4, 5, 1, 2, 3]
unique_numbers = set(numbers)
print("Unique numbers:", unique_numbers)
Output:
Unique numbers: {1, 2, 3, 4, 5}
Removing Duplicates from a List
This example shows how to use set() to remove duplicates from a list.
Example
# List with duplicate elements
numbers = [1, 2, 3, 4, 5, 1, 2, 3]
# Removing duplicates by converting to a set
unique_numbers = list(set(numbers))
print("List with duplicates removed:", unique_numbers)
Output:
List with duplicates removed: [1, 2, 3, 4, 5]
Performing Set Operations
This example demonstrates how to perform set operations like union, intersection, and difference.
Example
# Creating two sets
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
# Union
union_set = set_a.union(set_b)
print("Union:", union_set)
# Intersection
intersection_set = set_a.intersection(set_b)
print("Intersection:", intersection_set)
# Difference
difference_set = set_a.difference(set_b)
print("Difference:", difference_set)
Output:
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}
Iterating Over a Set
This example shows how to iterate over the elements of a set.
Example
# Creating a set of fruits
fruits = {"apple", "banana", "cherry"}
# Iterating over the set
print("Fruits:")
for fruit in fruits:
print(fruit)
Output:
Fruits:
cherry
banana
apple
Real-World Use Case
Membership Testing
In real-world applications, sets are often used for membership testing because of their efficient implementation.
Example
# List of students who attended the class
students = ["Alice", "Bob", "Charlie", "David"]
# Convert list to set for membership testing
student_set = set(students)
# Checking if a student attended the class
name = "Charlie"
if name in student_set:
print(f"{name} attended the class.")
else:
print(f"{name} did not attend the class.")
Output:
Charlie attended the class.
Finding Unique Elements
Sets can be used to quickly find unique elements from a list of items, such as user IDs or transaction IDs.
Example
# List of transaction IDs with duplicates
transactions = [101, 102, 103, 101, 104, 102, 105]
# Finding unique transaction IDs
unique_transactions = set(transactions)
print("Unique transaction IDs:", unique_transactions)
Output:
Unique transaction IDs: {101, 102, 103, 104, 105}
Conclusion
The set() function in Python is a versatile tool for creating sets, which are collections of unique and unordered elements. By using this function, you can efficiently handle tasks such as removing duplicates from a list, performing mathematical set operations, and testing membership. The set() function is particularly helpful in scenarios such as data cleaning, membership testing, and handling unique elements in your Python applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment