Python Sets

Understanding Python Sets

In Python, sets are a versatile data structure used for storing multiple items within a single variable. They are one of the four fundamental data types in Python, alongside Lists, Tuples, and Dictionaries. Each type serves unique purposes and has distinct features.

Sets are collections that are unordered, immutable*, and do not permit duplicate elements.

  • Unordered: The elements in a set do not follow a specific order. When you iterate over a set, the items may appear in a different order each time, and you cannot access them by index.
  • Immutable: Although you cannot modify individual elements directly, you can remove and add new items to a set.
  • No Duplicates: Sets automatically discard duplicate values.

Creating Sets

Sets are created using curly brackets:

book_set = {"1984", "Brave New World", "Fahrenheit 451"}
print(book_set)
# Output: {'1984', 'Fahrenheit 451', 'Brave New World'}

Keep in mind that since sets are unordered, the sequence of items is not guaranteed.

Set Items

Unordered

Sets do not maintain a specific order for their elements. As a result, you cannot access items using an index or key.

Immutable

While you cannot modify existing items in a set, you can remove items and add new ones:

book_set = {"1984", "Brave New World", "Fahrenheit 451"}
book_set.add("The Catcher in the Rye")
book_set.remove("Brave New World")
print(book_set)
# Output: {'1984', 'Fahrenheit 451', 'The Catcher in the Rye'}

No Duplicates

Duplicate entries are ignored in sets:

book_set = {"1984", "Brave New World", "Fahrenheit 451", "1984"}
print(book_set)
# Output: {'1984', 'Fahrenheit 451', 'Brave New World'}

In sets, values like True and 1 are considered identical:

book_set = {"1984", "Brave New World", "Fahrenheit 451", True, 1}
print(book_set)
# Output: {1, '1984', 'Fahrenheit 451', 'Brave New World'}

Similarly, False and 0 are treated as the same value:

book_set = {"1984", "Brave New World", "Fahrenheit 451", False, 0}
print(book_set)
# Output: {0, '1984', 'Fahrenheit 451', 'Brave New World'}

Determining Set Length

To find out how many items are in a set, use the len() function:

book_set = {"1984", "Brave New World", "Fahrenheit 451"}
print(len(book_set))
# Output: 3

Data Types in Sets

Sets can store elements of any data type, including strings, integers, and booleans:

set1 = {"1984", "Brave New World", "Fahrenheit 451"}
set2 = {2, 4, 8, 16, 32}
set3 = {True, False, True}
print(set1)
# Output: {'1984', 'Fahrenheit 451', 'Brave New World'}
print(set2)
# Output: {32, 2, 4, 8, 16}
print(set3)
# Output: {False, True}

Sets can also include various data types in a single set:

set1 = {"Book", 42, True, 56, "Magazine"}
print(set1)
# Output: {True, 42, 56, 'Book', 'Magazine'}

To check the data type of a set, use the type() function:

myset = {"1984", "Brave New World", "Fahrenheit 451"}
print(type(myset))
# Output: 

Using the set() Constructor

You can also create sets using the set() constructor:

book_set = set(("1984", "Brave New World", "Fahrenheit 451")) # Note the double parentheses
print(book_set)
# Output: {'1984', 'Fahrenheit 451', 'Brave New World'}

Python Collections Overview

Python offers several collection types:

  • List: Ordered and mutable. Allows duplicate items.
  • Tuple: Ordered and immutable. Allows duplicate items.
  • Set: Unordered, immutable*, and unindexed. No duplicate items.
  • Dictionary: Ordered** and mutable. No duplicate keys.

* Note: Although set items are immutable, you can remove and add items.

** Starting from Python 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries were unordered.

Understanding these data types helps in selecting the appropriate type for your data needs, enhancing both code efficiency and clarity.

Leave a Comment