Python – Sets

Python – Sets: In Python, a Set is a sort of data container that may hold numerous pieces of information in a single variable. It can include components of several data kinds. Because the elements of a set are not ordered, it is not feasible to retrieve a set’s element by index number. It also prohibits the use of multiple elements with the same value (no duplicate elements).

Create Set

A Python – Sets is formed by separating its items with a comma and surrounding them with a curly bracket. It may also be generated with the help of the set() method.

#Set with multiple datatypes
Info = {'John', 25, 'London'}  
print(Info)

#Creating set using constructor
colors = set(('Red', 'Blue', 'Green')) 
print(colors)

The output of the above code will be:

{'London', 'John', 25}
{'Blue', 'Red', 'Green'}

Access element of a Set

An index number cannot be used to access an element of a Set (indexing not allowed in a Set). A for loop, on the other hand, may be used to access components of a Set.

weekdays = {'MON', 'TUE', 'WED', 'THU', 'FRI'}
for day in weekdays:
  print(day)

The output of the above code will be:

MON
THU
TUE
FRI
WED

Modify value of an Element

It is not possible to change the value of a set’s elements, but we may add and remove them.

Add an element of a set:

Two methods can be used to add elements to a Set:

  • add() – add an element to the Set.
  • update() – add element(s) to the Set.
week = {'MON', 'TUE', 'WED'}
week.add('SUN')   # add this element in the set
print(week)
month = {'JAN', 'FEB', 'MAR', 'MAY'}
# multiple elements are updated in the set (with no duplication).
month.update(['JAN', 'NOV', 'DEC']) 
#Set is an unordered data container.
print(month)

The output of the above code will be:

{'SUN', 'WED', 'MON', 'TUE'}
{'NOV', 'MAY', 'JAN', 'MAR', 'DEC', 'FEB'}

 

Delete element of a set

To remove elements from a Set, you may use four methods and a keyword:

  • remove() – deletes specified element from the Set. Returns an error if the element is not present in the set.
  • discard() – deletes specified element from the Set. Returns no error if the element is not present in the set.
  • pop() – deletes last element from the Set. Please note that Set is an unordered data container therefore the last element is not predefined.
  • clear() – deletes all elements of the set.
  • del – deletes the set itself.
number = {10, 50, 100, 1000}
number.remove(50)    #delete 50 from the set.
print(number)

number = {10, 50, 100, 1000}
number.discard(50)   #delete 100 from the set.
print(number)

number = {10, 50, 100, 1000}
number.pop()         #delete last element from the set.
print(number)

number = {10, 50, 100, 1000}
number.clear()        #delete all elements from the set.
print(number)

number = {10, 50, 100, 1000}
del number            #delete set 'number' itself.
print(number)

The above code will give the following output:

{1000, 10, 100}
{1000, 10, 100}
{10, 100, 50}
set()

Traceback (most recent call last):
  File "Main.py", line 19, in <module>
    print(number)
NameError: name 'number' is not defined

Set Length

To determine the total number of entries in a list, tuple, set, or dictionary, use the len() function.

number = {10, 50, 100, 1000}
print(len(number))

The output of the above code will be:

4

Check an element in the Set

If the control statement is used to determine whether or not the set includes the supplied element.

colors = {'Red', 'Blue', 'Green'}
if 'white' in colors:
  print('Yes, white is an element of colors.')
else:
  print('No, white is not an element of colors.')

The above code will give the following output:

No, white is not an element of colors.

Join Sets

There are several ways to join Sets.

  • union(): union() method is used to create new set containing set1 and set2.
  • update(): update() method is used to add elements of a set in a given set.
colors = {'Red', 'Blue', 'Green'}
numbers = {10, 20}
mySet = colors.union(numbers)
print(mySet)

colors = {'Red', 'Blue', 'Green'}
numbers = {10, 20}
colors.update(numbers)
print(colors)

The output of the above code will be:

{'Green', 10, 20, 'Red', 'Blue'}
{'Green', 10, 20, 'Red', 'Blue'}

 

Also Read: Java – Constructors

Leave a Reply

Your email address will not be published. Required fields are marked *