Python Set

Set is an unordered collection of heterogeneous data. It allows us to store any type of data like integer, float, string, char, etc and we can store similar types of elements as well. Set stores comma-separated values enclosed within curly braces ({}).

Set does not maintain any order to store data, so, we cannot access its elements by using index value as list and tuple allow.

The set does not store duplicate elements. It means Set is a collection of unique elements.

In this topic, we are discussing about the set and its operations like creating a set, accessing set elements, updating set, removing set elements or iterating set, etc.

How to create Set in Python?

Let's start with an example to create a set that can store heterogeneous data. Here, we created several sets, set of numbers, set of strings, and set of mix values(integer, string, and float).

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# Create a set of String
fruits = {"Apple","Orange","Mango"}
print(fruits)

# Create a set of mix type values
mix = {12,"India","Delhi",11.20}
print(mix)
``````

Output:

{200, 122, 100, 400}
{'Mango', 'Apple', 'Orange'}
{11.2, 12, 'Delhi', 'India'}

Example: Access Set Elements in Python

After creating set, let's access its elements. Since Set does not maintain any order then we can not access its elements via index value. To access its elements, we can traverse its elements through for loop.

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# Access Elements
for val in numbers:
print(val)``````

Output:

{200, 122, 100, 400}
200
122
100
400

Example: Update Set in Python

We can not update Set but can add new elements to it. To add an element, we can use the add() method. See the example here.

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# Add new element to set
numbers.add(150)
print(numbers)``````

Output:

{200, 122, 100, 400}
{100, 200, 400, 150, 122}

Example: Remove Set Elements in Python

To remove elements from a set, we can use remove() method that removes the specified element from the set. Be careful while using remove() method, if the specified element is not present then Python throws an error "KeyError".

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# Remove element from set
numbers.remove(200)
print(numbers)

# Remove element from set
numbers.remove(250) # raise error
print(numbers)
``````

Output:

{200, 122, 100, 400}
{122, 100, 400}
KeyError: 250

Example: Traverse Set Elements using for loop

Since Set is a sequence of data, we can iterate it using for loop to traverse its elements. Here, we used for loop to print each element of the set.

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# Iterate Set elements
for val in numbers:
print(val)
``````

Output:

{200, 122, 100, 400}
200
122
100
400

Example: Find Length of Python Set

To get length of the set, we can use len() function that returns an integer which represents the number of elements in the Set. See the example here.

``````# Working with Python Set

# Create a set
numbers = {100,200,400,122}
print(numbers)

# find length of set
setLength = len(numbers)
print(setLength)``````

Output:

{200, 122, 100, 400}
4

Conclusion

Well, in this topic, we have learned to create a set, access, update and delete its elements. We created set and traverse its elements as well.

If we missed something, you can suggest us at - info.javaexercise@gmail.com

Trending