# Understand Set implementation in Python with Example

Set data structure of Python is powerful if you are working with collections of unique elements. Understanding the implementation of set in Python, including its time and space complexity, can help you write more efficient and effective code. In this post, we’ll explore the implementation of set in Python with examples to help you better understand this useful data structure.

## Why use set in python

There are so many reasons to use set in any python code. Like removing duplicates, efficient ways to search for a specific element, doing mathematical operations easily, faster than any other data types or oparations in Python language, etc.

## Set in python example

In this section, we will explore different ways to implement Set in python. First, let’s understand how to create a set in Python. Below is a simple example to do that.

```# Create a set of numbers
my_set = {1, 2, 3, 4, 5}

# Print the set
print(my_set)

# Output: {1, 2, 3, 4, 5}
```

Here in the above code, we created a set of numbers using the curly braces `{}`. Each element of a set needs to be separated by a `comma`.

So we understood what is set in Python and how to create it. Now let’s explore different cases where we can implement a set in Python.

#### Set in Pandas DataFrame

We can use `set()` function to extract unique values for a column of a pandas data frame. Below code is to do that.

```import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'fruit': ['apple', 'banana', 'orange', 'apple', 'orange']})

# Get the set of unique values in the 'fruit' column
fruit_set = set(df['fruit'])

# Print the set
print(fruit_set)

# Output: {'apple', 'orange', 'banana'}
```

In the above set implementation python code, we are creating a dataframe with only one column, then getting unique values of that fruit column using `set()` method of Python. Note that we can also use `unique()` method of a Pandas Series to get the same result.

#### Set in python dictionary

We can use set to get the unique keys or values of a Python dictionary. Below is one example script to do that.

```# Create a dictionary
my_dict = {'apple': 3, 'banana': 2, 'orange': 5, 'pear': 3, 'peach': 1}

# Get the set of unique keys
key_set = set(my_dict.keys())

# Print the set of unique keys
print(key_set)

# Output: {'pear', 'apple', 'banana', 'orange', 'peach'}

# Get the set of unique values
value_set = set(my_dict.values())

# Print the set of unique values
print(value_set)

# Output: {1, 2, 3, 5}
```

#### For loop in Set

We can apply for loop to iterate over the elements and also do some mathematical operations like sum elements with some value for a set in Python. Let’s explore that in the below code.

```# Create a set
my_set = {1, 2, 3, 4, 5}

# Create a new set with each element of my_set increased by 1
new_set = set()
for element in my_set:

# Print the new set
print(new_set)

# Output: {2, 3, 4, 5, 6}
```

#### Sum of set of elements

To get the sum value of a set of elements we can simply use `sum()` function of Python.

```# Create a set of numbers
my_set = {1, 2, 3, 4, 5}

# Get the sum of the numbers using the sum() function
total = sum(my_set)

# Print the sum to the console
print(total)

# Output: 15
```

#### Insert element to a set

We can insert one or more elements to a set using `add()` or `update()` method in Python. Let’s understand that using the below code.

```# Create a set
my_set = {1, 2, 3}

# Print the set
print(my_set)

# Output: {1, 2, 3, 4}

# Add multiple elements to the set using the update() method
my_set.update([5, 6, 7])

# Print the set
print(my_set)

# Output: {1, 2, 3, 4, 5, 6, 7}
```

#### Remove element from a Set

In the below code we first create an empty set then adding and removing some elements to the set and finally printing the updated set.

```# Create an empty set
my_set = set()

# Create a set with elements
my_set = {1, 2, 3}

# Add an element to the set

# Remove an element from the set
my_set.remove(3)

# Print updated set
print(my_set)

# Output: {1, 2, 4}
```

#### List to set in python

To convert list to set, we can simply use built in `set()` function in Python.

```# Create a list of numbers
my_list = [1, 2, 2, 3, 3, 3]

# Convert the list to a set
my_set = set(my_list)

# Print the set to the console
print(my_set)

# Output: {1, 2, 3}
```

In above set implementation python code, we first created a list of numbers with duplicate values. Then converted the list to a set using the `set()` function. You can see the output contains only the unique values from the list. This is because sets in Python are unordered and do not allow duplicates.

Also Read:  Python | List all files in a Directory

Note that I am using only numbers in the set for all demo python code, you can also use strings to a set as an element.

#### Set in python tuple

We can create or convert to a set from a tuple using the built-in `set()` function.

```# Create a tuple of numbers
my_tuple = (1, 2, 2, 3, 3, 3)

# Convert the tuple to a set
my_set = set(my_tuple)

# Print the set to the console
print(my_set)

# Output: {1, 2, 3}
```

#### regex in Set

We can use regular expressions (regex) in sets to match any character from a specific set of characters. In the below code we will try to match and print available values between 3 to 5 in a set.

```import re

# Create a set with elements
my_set = {1, 2, 3, 4, 5, 6}

# Convert the set to a string
my_string = str(my_set)

# Match any digit (0-9)
pattern = re.compile("[3-5]")

# Find all matches in the string
matches = pattern.findall(my_string)

# Print all matched characters to the console
print(matches)

# Output: ['3', '4', '5']
```

#### Lookup in Sets

You can use the `in` operator to check if an element is available in a set or not. The `in` operator returns a boolean value of `True` if the element is in the set or `False` otherwise. Below is the sample code to understand Lookup in Python set oparations.

```# Create a set with some elements
my_set = {1, 2, 3, 4, 5}

# Check if an element is in the set
if 3 in my_set:
print("3 is in the set")
else:
print("3 is not in the set")

# Check if another element is in the set
if 6 in my_set:
print("6 is in the set")
else:
print("6 is not in the set")
```
``````3 is in the set
6 is not in the set``````

## FAQs

In this section let me answer some common questions about set method in python.

#### Is set in python ordered?

No set in Python is not ordered. Now why sets are unordered in python? The reason why `set` is unordered in Python is due to the way sets are implemented internally.

Set is implemented using hash table. Now hash table is a data structure which is specially designed for fast insertion, deletion, and lookup of elements based on a key. In a hash table, elements are stored in buckets based on their hash values, rather than their order of insertion. This kind of technique can be useful for fast lookup of elements, but it can not maintain the order of the elements.

If you want to maintain the order of elements in a collection set will not at all a good choice. In that case you can use a list or a tuple.

#### Set in python is mutable or immutable

The Python documentation says that if you can not modify any element then it is immutable. As we have seen in above examples, we can add, remove, and modify elements in a set. So set in Python is mutable.

#### Time complexity of Set in Python

As per python wiki, the data structure of Set is implemented using `hash table`. Now time complexity of a hash table is around O(1) for basic operations like add, remove, searching elements, etc.

For some operations like delete() the time complexity of a set operation may take some time around O(n), where n is the number of elements in the set.

#### set vs list performance in Python

Set and list both used to store collections of elements in Python, but they have different characteristics and performance level.

Also Read:  Python Find in List: Complete Guide

Sets are faster than lists for membership tests (searching some element in a collection), while lists are faster for maintaining order and accessing elements at specific positions.

In short, if you want to maintain the order of your collection of elements then you should use list. But if you do not want to maintain the order and you just looking for a faster solution to search any element or perform any mathematical operations then set is a good choice.