Py10: Set

Set is a model for a collection of things. A set contains a number, or string which are unique and stored in no particular order.

Introduction

In python, a set is an unordered list of elements. The elements stored in a set in unique, which means there is no repetitive element in a set. A set is immutable, so we cannot change the value of an element.

We can add new items into a set and an element can be removed from a set. Python uses a curly bracket to define a set.

Declare Set

To define a set, we list elements inside curly brackets ({ })

set_name = {item1, item2,...,itemN}

Dictionary also uses curly brackets so we cannot define a set like follow because it creates a dictionary, not a set.

set_name = {} # this creates a dictionary not a set

Example

a_set = {1, 2, 3, 4, 5, 6, 7}

print(type(a_set)) # returns data type
print(a_set)       # prints items in set

Output

<class 'set'>
{1, 2, 3, 4, 5, 6, 7}
>

Getting number of elements

Use the len() function for getting information about how many elements are there in a set.

a_set = {'small','medium','large'}

print(len(a_set)) # there are 3 items in the set

Output

3
>

in membership

To check if a key is one of the elements in a list, use in membership operator.

a_set = {'small','medium','large'}

exists = 'tiny' in a_set           # 'tiny' not in the set
print("'tiny' is in set:", exists) 

exists = 'medium' in a_set         #'medium' is in the set
print("'medium' is in set:", exists)

Output

'tiny' is in set: False
'medium' is in set: True
>

Adding an Element

To add a new element into a set, use the add () function.

a_set = {'small','medium','large'}

a_set.add('x-large') # adds new item into set

print(a_set)         # prints items in set.

Output

{'x-large', 'large', 'small', 'medium'}
> 

Remove an Element

remove()

To remove an element from a set, by using the remove() function.

a_set = {'small','medium','large'}

a_set.remove('small') # removes 'small' from set

print(a_set)          # prints items in set

Output

{'large', 'medium'}
> 

trying to remove an element that does not exist in a set will cause KeyError

a_set = {'small','medium','large'}

a_set.remove('x-large') # error occurs if wrong key is used

print(a_set)

Output

Traceback (most recent call last): File "<string>", line 3, in <module> KeyError: 'x-large'

To remove an element from a set, by using the discard() function. If the element does not exist in the set, the error is handled smoothly.

a_set = {'small','medium','large'}

a_set.discard('x-large')# remove element without raising error if key not in set.

a_set.discard('medium') # 'medium' removed like normal
print(a_set)

Output

{'large', 'small'}
> 

pop()

to remove and return an element from a set, use the pop() function. This function will return an element, not in a specific order.

# to return a value while removing an element from set. 
# returns random value from set because set is unordered list.

a_set = {'small','medium','large'}

size = a_set.pop() # returns random value from set
print(size)        # print returned item

size = a_set.pop() # returns random value from set
print(size)        # print returned item

size = a_set.pop() # returns random value from set
print(size)        # print returned item

print(a_set)       # print items in set

Output

medium
large
small
set()
>

clear()

To clear the elements in a set, we use the clear() function.

a_set = {'small','medium','large'}

size = a_set.clear() # clears the set

print(len(a_set))    # print the size of set

print(a_set)         # print items in set

Output

0
set()
> 

For Loop

Using Loop to access the elements in a set. Set is an iterable, so it can be used in for loop.

a_set = {'small','medium','large'}

for size in a_set: # iterate the set, item by item
    print(size)    # print item

Output

small
large
medium
> 

Using enumerate() function to return elements from a set with an index. Notice that the element is unordered.

a_set = {'small','medium','large'}

for size in enumerate(a_set): # enumerate the set, then iterate
    print(size)               # print item & index

Output

(0, 'medium')
(1, 'small')
(2, 'large')
> 

to access the index return by enumerate() function, define two variables, one to hold the index and another to hold a value of an element.

a_set = {'small','medium','large'}

for index, size in enumerate(a_set):
    print(index, ":",size)

Output

0 : large
1 : medium
2 : small
> 

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net