Python set() function with Examples

Python is a high-level, multi-purpose programming language that has gained immense popularity over the years. Its extensive libraries make it a perfect choice for web and app development, data analysis, machine learning, and more. One such useful tool within Python is the `set()` function.

In this article, we will take an in-depth look at the Python `set()` function, provide examples of how to use it, and answer some frequently asked questions.

What is a Python set() function?

In Python, a set is an unordered collection of unique elements. The `set()` function is used to create a set datatype in Python. The `set()` function is a built-in Python function that returns a set object.

The syntax to create a set using the `set()` function is as follows:

```
set(iterable)
```

Where `iterable` is the list, tuple, or any other sequence object. It can also be a string.

Examples

Let’s take a look at some examples to understand the usage of the `set()` function.

Example 1:

```
fruits = set(["apple", "banana", "cherry", "apple"])
print(fruits)
```
Output:
```
{'apple', 'banana', 'cherry'}
```
In this example, we have created a set of fruits using the `set()` function. We have passed a list of fruits as an argument. As we can see, the duplicate "apple" has been removed.

Example 2:

```
numbers = set(range(1,6))
print(numbers)
```
Output:
```
{1, 2, 3, 4, 5}
```

In this example, we have created a set of numbers using the `set()` function. We have passed a range from 1 to 6 as an argument. As we can see, the resulting set has only unique elements.

Example 3:

```
string = "Hello World"
set_string = set(string)
print(set_string)
```
Output:
```
{' ', 'W', 'e', 'r', 'd', 'l', 'o', 'H'}
```

In this example, we have created a set of string using the `set()` function. We have passed a string as an argument. As we can see, the set includes only unique characters from the string.

FAQs

1. What is the difference between a list and a set in Python?

A list is an ordered collection of elements, and it allows duplicates, whereas sets are unordered collections that contain only unique objects.

2. Can we add elements to a set in Python?

Yes, we can add elements to a set using the `add()` method.

3. How can we remove elements from a set in Python?

We can remove elements from a set using the `remove()` method.

4. What is the difference between update() and add() methods in Python?

The `add()` method is used to add a single element to a set, whereas the `update()` method is used to add multiple elements to the set.

5. Can we sort a set in Python?

No, we cannot sort a set in Python as sets are unordered. However, we can convert it to a list and then sort the list.

Conclusion

In this article, we have seen how to create a set datatype using the Python `set()` function. We also provided various examples of using this function. The `set()` function is a useful tool in Python for working with unique and unordered collections. Its ease of use and powerful functionality makes it an essential skill for any Python developer.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts

Write for us

Write for Us

Are you looking for guest posting opportunities? I guess you’re looking for blogs about Digital Marketing, SEO, or Freelancing that

Read More