Introduction

In this article we will introduce one more built-in Data Structure in Python-Sets. Previously we read about other built-in Data Structures in Python-Tuples, Lists and Dictionaries. We will learn about Data Structure in Python-Sets, functions used, iterations and operators. Lets start with the definition.

Few quick words: I will suggest you to download the latest version of Python which will help you to practice coding python programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.

I also suggest you to visit the article here to learn the basic concepts of Blockchain.

Data Structures in Python-Sets-Definition

  • Sets are collection of items which are iterable, mutable, unordered and have no repeated items or elements.
  • Set is a collection which is both unordered and unindexed. They are used to store multiple items in a single variable.
  • Unlike Lists, Sets are unordered. The primary reason for using Sets, as opposed to Lists, is the more optimized method of finding whether an item is contained in the Set or not.
  • Syntax: {key1,key2,key3,…….}

set( )

  • You can create a Set using the set( ) function or enclosing elements in curly brackets ({ }).
  • Like Dictionaries, Sets also use { }. The difference is that Dictionaries have keys and values attached to them, whereas Sets only have keys.

Output:

  • We can create a Set from a string, tuple, lists and dictionaries. It will remove any duplicate values.

Output:

Now lets try and make a set from a list.

Output:

len( ): to get the size of a set

  • The len( ) function is used to determine the size of a set.
  • For the set, set_test, defined in the above example, let us find the length using the len( ).

Output:

add( ): to add an item to a set

  • We can add any new item in a set using the add( ) function.

Output:

{1,2,3}
{1,2,3,4}

remove( ):to remove an item from a list

  • We can delete any item from a set by using the remove( )

Output:
{1,2,3,4}

{1,2,3}

Iterate with for and in

  • Like in a tuple, list or a dictionary, we can iterate over the elements of a set using for and in.

Output:

Test a value with in

  • Let us define a dictionary with the surnames as the keys and first names as the corresponding values.

Output:

  • Let’s say we want to check all the surnames with the first names Nitin.

Output:

  • Similarly if we want to list down all the surnames with first names Barry.

Output:

  • When we want to list down all the surnames with the first-names Nitin but except Riley (since may be Riley a UK surname does not suit with the Indian first name Nitin)

Output:

Operators (a&b, a|b, a-b, a^b, a<=b, a<b, a>=b)

  • Let us again take the previous example.
  • If we want to list all the surnames with either ‘Nitin’ or ‘Joe’ as the first names, then we will do something like the below.

Output:

We could have used the ampersand ‘&’ operator also.

Here the & operator takes the intersection of the two sets compared, i.e., names (set of all the first names) and {‘Nitin,’Joe’}.

Similarly when we want to find all the surnames with ‘Nitin’ as the first names but not ‘Joe’ nor ‘Barry’.

Output:

  • & (ampersand) is one of the operators used in Sets. Other operators are I ,-.
  • & is used for intersection, same as the function intersection( )
  • I is used for Union (members of either Set) of two Sets. union( ) does the same.
  • is used for the difference (member of first set but not the second) of two Sets.
  • Let us take two sets a and b
  • The intersection of the two sets

Output:

or we can use the intersection( ) function

Output:

  • The union of the two sets by using | operator or the union( ) function

Output:

  • The difference of the two sets by using the operator or the difference( ) function.

Output:

  • Other less frequently used operators in Sets are ^,<=, <, >=
  • The exclusive (items in one set or the other, but not in both) uses ^ operator. You can use the symmetric_difference( ) function also
  • Use <= to check whether one set is a subset of the other set. You can also use the issubset( ) function.

Output:

  • Use >= to check whether one set is a superset of another set. In that case, all the elements of the second set are also the elements of the first set. You can use the issuperset( ) function also.

Output:

Leave a Reply