Python Sets

Python has 4 built-in data types that are used to store collections of data, They are  Python Sets

  1. List
  2. Tuple
  3. Set
  4. Dictionary

In this tutorial we will learn about Sets.

a set is simply a collection of unordered items. The set itself is mutable, but the set elements are immutable. however, we can add remove elements from a set freely. in most data structures, elements are indexed. however, set elements are not indexed. this makes it impossible for us to perform operations that target specific set element.

• Set is mutable in nature so we can add, update, pop , remove, clear

• Unordered and unindexed collection of items

• A set does not hold duplicate items

• Set elements are unique. duplicate element are not allowed

• Set element are immutable (cannot be changed)

• Set itself is mutable. we can add or remove items from it.

• We can not perform slicing in sets as well and indexing is also not supported.

• Element of set are immutable or editable but set itself is mutable or editable.

We write the items of set inside the curly braces { }

my_set = {"apple", 2.5, "hello", 50}
print(my_set) # Unordered 
{2.5, 50, 'apple', 'hello'}

Typeof set:

We used type() method to check their type.

my_set = {"apple", 2.5, "hello", 50}
print(type(my_set)
<class 'set'>

Add a new element :

add() method is used add a new item to the set.

my_set = {"apple", 2.5, "hello", 50}
my_set.add(5j)

print(my_set) # Unordered  
{2.5, 5j, 'apple', 50, 'hello'}

Delete an existing element:

remove() method is used remove an item in set.

my_set = {"apple", 2.5, "hello", 50}
my_set.remove("apple")

print(my_set) # Unordered 
{2.5, 50, 'hello'}

looping through the set elements:

my_set = {"apple", 2.5, "hello", 50}
for i in my_set:    
    print(i) 
apple
2.5
50
hello

Duplicates Not Allowed:

Sets cannot allow two items with the same value.

my_set = {"apple", 2.5, "hello", 50}
my_set.add(2.5)

print(my_set) # Unordered 
{'hello', 2.5, 50, 'apple'}

List vs Set

Functional differences:

  • List:
    • The order of elements is well defined,
    • Duplicates are possible,
    • Any object can participate.
  • Set:
    • The order of the elements is unknown,
    • Elements are unique,
    • Only hashable objects can be inserted (tuples, and numbers, but not lists).
      Can be user defined objects that define __hash__() and __eq__() methods, and never change.

Most used set methods

MethodDescription
remove( )Used to remove an element from the Set
copy( )Used to make a copy of the Set
pop( )Used to remove an element from the Set
clear( )Used to remove all the elements from the Set
add( )Used to add an element to the Set
difference( )Used to make a Set that has the difference between two Sets
discard( )Used to remove an Item from the Set
union( )Used to take the values from both the sets and joins them
update( )Used to update the current Set by getting the values from the other Set

  1. Set is simply a collection of unordered items. The set itself is mutable, but the set elements are immutable.
  2. Set elements are unique. duplicate element are not allowed