# Python Set With Examples [Latest] In this tutorial we are going to learn Python Set. In our previous article we learnt about Python String. You can learn it from here. ## Python Set

Python Set is an unordered collection of unique elements. Suppose you have a list and you need only the unique items of the list you can use Python Set. Similarly, if you need only unique items from input, Python set can help you to do so. You can add or delete items from it.

You can initialize a set by placing elements in between curly braces. Like other sequences, one set can have elements of multiple data-types. Moreover, you can also create a set from a list by using set() function. The following example will give you some idea about initializing a set.

The output will be

### Adding Elements to Python Set

In previous example, we learned how to initialize Python set directly. Suppose we need to add element to set, we can do so by using add() function. But this function can add a single element. If you want to add iterable elements like list or set, you can do so by using update() function. The following example will help you understand the thing

The output of the following code will be

### Remove Elements from Python Set

There are two functions to remove elements from Python Set. One is remove() and another is discard() function. If the element you are trying to remove is not in the set, the remove() function will raise exception for this. But the discard function will not do anything like this. The following code will show you those

You will find the output be like,

### Python Set Operations

You might be familiar with some mathematical set operations like union, intersection, difference. We can also do those using Python set. Now, we will learn how to do that.

### Python Set Union

Union is the operation to merge two sets. That means, union will create another set that contains all unique elements of two sets. For example, {1, 2, 3, 4} and {2, 3, 5, 7} are two sets. If we do union operation over them, we get {1, 2, 3, 4, 5, 7}. We can obtain this by using union() function.

### Python Set Intersection

Again, intersection is the operation to get the common unique elements of two sets. For example, {1, 2, 3, 4} and { 2, 3, 5, 7} are two sets. If we intersect them, we get, {2, 3}. The intersection operation is done by intersection() function.

### Python Set Difference

Now, difference operation compares two sets and creates a new set containing items from set A which are not common in set B. Suppose, we have two sets, A = {1, 2, 3, 4} and B = {2, 3, 5, 7}. Then, A – B operation will generate {1, 4}. Moreover, B – A will generate {5, 7}. The difference operation is done by difference() function..

The following code will give you idea about how to do these set operation in python programming.

The output you get will be like this

So, that’s all for today. Hope that you learned well about Python Set. For any further query you can just write your query in the comment box. We will answer you.

Reference: Official Documentation
{"ft_lang_label":"__label__en","ft_lang_prob":0.76205134,"math_prob":0.87243444,"size":5370,"snap":"2023-14-2023-23","text_gpt3_token_len":1658,"char_repetition_ratio":0.16734998,"word_repetition_ratio":0.14209402,"special_character_ratio":0.34841713,"punctuation_ratio":0.1751886,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9759652,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-05T18:04:25Z\",\"WARC-Record-ID\":\"<urn:uuid:a42d50d1-9954-4ff4-b0cc-8a42c746e45d>\",\"Content-Length\":\"159301\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fd1521a1-59cc-4abd-86fe-5c32040edd5a>\",\"WARC-Concurrent-To\":\"<urn:uuid:69765567-1aca-47bc-a7e7-86c892a6f9e2>\",\"WARC-IP-Address\":\"104.21.8.167\",\"WARC-Target-URI\":\"https://all-learning.com/python-set/\",\"WARC-Payload-Digest\":\"sha1:XHU47RQZ7FK6XMNQUVAF4MLZ7Q4ZCOI7\",\"WARC-Block-Digest\":\"sha1:B7U3I6GJKCLQSHP5WCXHJ4KVB425X3DQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224652149.61_warc_CC-MAIN-20230605153700-20230605183700-00028.warc.gz\"}"}