{"id":91,"date":"2024-01-25T10:09:12","date_gmt":"2024-01-25T10:09:12","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=91"},"modified":"2024-01-25T10:09:13","modified_gmt":"2024-01-25T10:09:13","slug":"python-set","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-set\/","title":{"rendered":"Python Set"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python&nbsp;<code>Set<\/code>&nbsp;type and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python Set type<\/h2>\n\n\n\n<p>A Python set is an unordered list of immutable elements. It means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elements in a set are unordered.<\/li>\n\n\n\n<li>Elements in a set are unique. A set doesn\u2019t allow duplicate elements.<\/li>\n\n\n\n<li>Elements in a set cannot be changed. For example, they can be\u00a0numbers,\u00a0strings, and\u00a0tuples, but cannot be\u00a0lists\u00a0or\u00a0dictionaries.<\/li>\n<\/ul>\n\n\n\n<p>To define a set in Python, you use the curly brace\u00a0<code>{}<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Python programming','Databases', 'Software design'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>Note a dictionary also uses curly braces, but its elements are key-value pairs.<\/p>\n\n\n\n<p>To define an empty set, you cannot use the curly braces like this:<code>empty_set = {}<\/code><\/p>\n\n\n\n<p>\u2026because it defines an empty dictionary.<\/p>\n\n\n\n<p>Therefore, you need to use the built-in\u00a0<code>set()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>empty_set = set()<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>An empty set evaluates to False in\u00a0Boolean\u00a0context. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = set() if not skills: print('Empty sets are falsy') <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Empty sets are falsy <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>In fact, you can pass an\u00a0iterable\u00a0to the\u00a0<code>set()<\/code>\u00a0function to create a set. For example, you can pass a list, which is an iterable, to the\u00a0<code>set()<\/code>\u00a0function like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = set(&#91;'Problem solving','Critical Thinking']) print(skills)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'Critical Thinking', 'Problem solving'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>Note that the original order of elements may not be preserved.<\/p>\n\n\n\n<p>If an iterable has duplicate elements, the\u00a0<code>set()<\/code>\u00a0function will remove them. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>characters = set('letter') print(characters)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'r', 'l', 't', 'e'} <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the string&nbsp;<code>'letter'<\/code>&nbsp;has two e and t characters and the set() removes each of them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting sizes of a set<\/h2>\n\n\n\n<p>To get the number of elements in a set, you use the built-in&nbsp;<code>len()<\/code>&nbsp;function.<code>len(set) <\/code><small>Code language: JavaScript (javascript)<\/small><\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>ratings = {1, 2, 3, 4, 5} size = len(ratings) print(size) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<code>5<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Checking if an element is in a set<\/h2>\n\n\n\n<p>To check if a set contains an element, you use the&nbsp;<code>in<\/code>&nbsp;operator:<code>element in set<\/code><small>Code language: JavaScript (javascript)<\/small><\/p>\n\n\n\n<p>The\u00a0<code>in<\/code>\u00a0operator returns\u00a0<code>True<\/code>\u00a0if the set contains the element. Otherwise, it returns\u00a0<code>False<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>ratings = {1, 2, 3, 4, 5} rating = 1 if rating in ratings: print(f'The set contains {rating}')<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>The set contains 1<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>To negate the\u00a0<code>in<\/code>\u00a0operator, you use the\u00a0<code>not<\/code>\u00a0operator. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>ratings = {1, 2, 3, 4, 5} rating = 6 if rating not in ratings: print(f'The set does not contain {rating}') <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>The set does not contain 6<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding elements to a set<\/h2>\n\n\n\n<p>To add an element to a set, you use the\u00a0<code>add()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set.add(element) <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Python programming', 'Software design'} skills.add('Problem solving') print(skills) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'Problem solving', 'Software design', 'Python programming'} <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing an element from a set<\/h2>\n\n\n\n<p>To remove an element from a set, you use the\u00a0<code>remove()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set.remove(element) <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skills.remove('Software design') print(skills)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'Problem solving', 'Python programming'}<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>If you remove an element that doesn\u2019t exist in a set, you\u2019ll get an error (<code>KeyError<\/code>). For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skills.remove('Java') <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>KeyError: 'Java'<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>To avoid the error, you should use the\u00a0<code>in<\/code>\u00a0operator to check if an element is in the set before removing it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} if 'Java' in skills: skills.remove('Java') <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>To make it more convenient, the set has the\u00a0<code>discard()<\/code>\u00a0method that allows you to remove an element. And it doesn\u2019t raise an error if the element is not in the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set.discard(element) <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skills.discard('Java') <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Returning an element from a set<\/h2>\n\n\n\n<p>To remove and return an element from a set, you use the&nbsp;<code>pop()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Since the elements in a set have no specific order, the&nbsp;<code>pop()<\/code>&nbsp;method removes an unspecified element from a set.<\/p>\n\n\n\n<p>If you execute the following code multiple times, it\u2019ll show a different value each time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skill = skills.pop() print(skill) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Removing all elements from a set<\/h3>\n\n\n\n<p>To remove all elements from a set, you use the&nbsp;<code>clear()<\/code>&nbsp;method:<code>set.clear() <\/code><small>Code language: JavaScript (javascript)<\/small><\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skills.clear() print(skills) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>set() <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Frozen a set<\/h2>\n\n\n\n<p>To make a set immutable, you use the built-in function called\u00a0<code>frozenset()<\/code>. The\u00a0<code>frozenset()<\/code>\u00a0returns a new immutable set from an existing one. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} skills = frozenset(skills) <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>After that, if you attempt to modify elements of the set, you\u2019ll get an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills.add('Django') <\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>AttributeError: 'frozenset' object has no attribute 'add'<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Looping through set elements<\/h2>\n\n\n\n<p>Since a set is an iterable, you can use a for loop to iterate over its elements. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} for skill in skills: print(skill) <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Software design Python programming Problem solving<\/code><\/code><\/pre>\n\n\n\n<p>To access the index of the current element inside the loop, you can use the built-in\u00a0<code>enumerate()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} for index, skill in enumerate(skills): print(f\"{index}.{skill}\") <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0.Software design 1.Python programming 2.Problem solving<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>By default, the index starts at zero. To change this, you pass the starting value to the second argument of the\u00a0<code>enumerate()<\/code>\u00a0function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>skills = {'Problem solving', 'Software design', 'Python programming'} for index, skill in enumerate(skills, 1): print(f\"{index}.{skill}\")<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1.Python programming 2.Problem solving 3.Software design<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python&nbsp;Set&nbsp;type and how to use it effectively. Introduction to the Python Set type A Python set is an unordered list of immutable elements. It means: To define a set in Python, you use the curly brace\u00a0{}. For example: Note a dictionary also uses curly braces, but its elements [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-7-sets"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/91\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}