Python Sets



myset = {"apple", "banana", "cherry"}


Set

Sets are used to store multiple items in one place.

Set is one of 4 data types built into Python that are used to store data collections, the other 3 List, Tuple, and a dictionary, all with different attributes and uses.

Set a random, unmodified *, and unidentified collection.


*Note: Default items do not change, but you can delete items and add new items.


Sets are written in italics.


Example

Create a Set:

thisset = {"apple", "banana", "cherry"}
print(thisset)

Note: The sets are not in order, so you cannot be sure which system will appear.



Set Items

The set items are random, unchanged, and do not allow duplicate values.


Unordered

Non-order means that items in the set do not have a specified order.

Default items may appear in a different format each time you use them, and cannot be referenced by index or key.



Unchangeable

The preset items do not change, which means we will not be able to change the items once they have been created.


Once a set has been created, you can't change its elements, but you can remove items and add new ones.



Duplicates Not Allowed

Sets cannot have two items of the same value.


Example

Duplicate values will be ignored:

thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)


Get the Length of a Set

To determine how many items a set has, use the len() method.


Example

Get the number of items in a set:

thisset = {"apple", "banana", "cherry"}

print(len(thisset))


Set Items - Data Types

Set items can be any type of data:


Example

String, int and boolean data types:

set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}

The set may contain different types of data:


Example

A set with strings, integers and boolean values:

set1 = {"abc", 34, True, 40, "male"}


type()

From Python's point of view, sets are defined as data sets:


<class 'set'>


Example

What is the data type of a set?

myset = {"apple", "banana", "cherry"}
print(type(myset))


The set() Cosntructor

It is also possible to use the set() to create the set.


Example

Using the set() constructor to make a set:

thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)


Python Collections (Arrays)

There are four types of data collection in Python editing language:

  • The list is a ordered and flexible collection. Allows duplicate members.
  • Tuple is a ordered and unchanged collection. Allows duplicate members.
  • Set is a random, unstable *, and unidentified collection. No duplicate members.
  • The dictionary is a ordered order ** and is flexible. No duplicate members.

* Set items do not change, but you can delete items and add new items.

** Starting with version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.


When choosing a type of collection, it is helpful to understand the characteristics of that type. Choosing the right type of specific data set may mean keeping the definition, and, it may mean increased efficiency or security.



Python - Access Set Items



Access Items

You can not access the set items by pointing to the index or key.

But you can enter the settings using a loop, or ask if the specified value is in the set, by using the key in.


Example

Loop through the set, and print the values:

thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)


Example

Check if "banana" is present in the set:

thisset = {"apple", "banana", "cherry"}

print("banana" in thisset)


Change Items

Once a set has been created, you can not change its elements, but you can add new items.