# Set

### Set

Mutable collection data type. Set can be modified.

Set has unique features:

Set does not contain duplicate elements, every element in a set is unique

Membership testing against a set (search if a value is an element of a set) is hashmap, meaning search against a set is faster than against a list, which is linear search.

Example:

s={1,2,3,4,5,5}

You notice there are two 5s

When you print out s, you only see one 5, which is

{1, 2, 3, 4, 5}

You can convert a list to a set by using function set(), by doing so, you eliminate duplicate elements

l=\[1,2,3,4,5,5]

set(l)

l=\[1,2,3,4,5,5]

set(l)

{1, 2, 3, 4, 5}

To create an empty set, use function set()

s=set()

To add an element ‘a’ into s:

s.add(‘a’)

To delete element ‘a’ from s:

s.remove(‘a’)

Need to note:

Order of elements in a set is not guaranteed and consistent


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://george-jen.gitbook.io/data-science-and-apache-spark/python-set.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
