---
title: "What is meant by set in python?"  
description: "What is meant by set in python?"  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-04-20  
canonical: https://www.mindstick.com/forum/157624/what-is-meant-by-set-in-python  
category: "python"  
tags: ["python"]  
reading_time: 2 minutes  

---

# What is meant by set in python?

What is meant by set in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?Give suitable example.

## Replies

### Reply by Aryan Kumar

In Python, a set is an unordered collection of unique elements. It is similar to a list or a tuple, but unlike those data types, a set does not allow duplicate values. Sets are implemented using a hash table, which allows for fast membership testing and set operations such as intersection and union.

To create a set in Python, you can use curly braces **{}** or the built-in **set()** function. For example:

```python
my_set = {1, 2, 3, 4, 5}
```

### Reply by Krishnapriya Rajeev

Sets are one of the 4 *built-in data types* in Python; it is an unordered collection data type that is iterable, mutable, and does not allow for duplicate elements.

Example:

```plaintext
set1 = {"Delhi","Kolkata","Mumbai"}
```

The advantage of using a set as opposed to a list is that it uses a hash table for performing set operations, which is more optimized.

Sets are *unordered*, as a result of which items *cannot be accessed using indexes* as in the case of lists. However, we can loop through the items using a loop.

Example:

```plaintext
set1 = {"Delhi","Kolkata","Mumbai"}
for x in set1:
	print(x)

OUTPUT:
Delhi
Mumbai
Kolkata
```

We can *add elements* to a set as follows:

```plaintext
set1.add("Chennai")
```

To *remove* a specific element:

```plaintext
set1.remove("Delhi")
```

To *clear all elements* from the set:

```plaintext
set1.clear()
```


---

Original Source: https://www.mindstick.com/forum/157624/what-is-meant-by-set-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
