Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create and Check an Empty Set in Python

  • 20 Feb, 2025
  • Com 0
Check if a set is empty in Python

To create an empty Set in Python, you can use the set() Constructor without any arguments. It looks like this: empty_set = set().

Creating an empty set in Python

empty_set = set()

print(empty_set)

# Output: set()

However, do not use {}(curly braces) because it will initialize an empty dictionary, not set.

empty_set = {}

print(empty_set)
print(type(empty_set))

# Output: {}
# <class 'dict'>

So, what you are initializing is not a Set, but in reality, it is a Dictionary that only accepts key/value pairs.

Check if a Set is empty

The easiest way to check if a Set is empty is to check its length with len(empty_set) == 0. The length of an empty object is always 0. If we get True, the Set is empty; otherwise, it is not.

Creating and checking if a Set is Empty

empty_set = set()

print(len(empty_set) == 0)

# Output: True

If the Set has some elements, the len() method will return a number other than 0.

filled_set = {11, 21, 19}

print(len(filled_set) == 0)

# Output: False

This approach is very efficient and highly recommended.

Implicit boolean check

You can also use the ‘if not’ operator to check whether a set is empty or not. For example, if not empty_set, that would be True if the set is empty.

filled_set = {11, 21, 19}

if not filled_set:
    print("Set is empty")
else:
    print("Set is not empty")

# Output: Set is not empty

Let’s check with an actual empty set:

empty_set = set()

if not empty_set:
    print("Set is empty")
else:
    print("Set is not empty")

# Output: Set is empty

It is the Pythonic way that is readable and concise.

Post Views: 23
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Set to List in Python
How to Get the Length of a Set in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend