[Python tutorial] Chapter 43 collection

Posted by upperbid on Sat, 05 Feb 2022 11:05:31 +0100

In this article, we begin to learn the Set data types and related operations in Python.

Collection data type

A collection in Python is an unordered list of immutable elements. This means:

  • The elements in the collection have no order.
  • The elements in the collection are unique. Duplicate elements are not allowed in the collection.
  • Elements in a collection can only be immutable objects, such as numbers, strings, and tuples. Elements in a collection cannot be lists or dictionaries.

Define collections in Python using curly braces ({}), for example:

skills = {'Python programming','Databases', 'Software design'}

A dictionary is also defined with braces, but its elements are key value pairs.

Defining an empty collection cannot use braces of the following form:

empty_set = {}

Because the above code means to define an empty dictionary.

To define an empty set, you need to use the built-in set() function:

empty_set  = set()

When calculating Boolean values, the result of an empty set is False. For example:

skills = set()

if not skills:
    print('Empty sets are falsy')

The output results are as follows:

Empty sets are falsy    

In fact, we can pass a traversable object to the set () function to create a collection. For example, the following code creates a set using a list and the set() function:

skills = set(['Problem solving','Critical Thinking'])
print(skills)

The output results are as follows:

{'Critical Thinking', 'Problem solving'}

Note that the elements in the collection may no longer retain the order in the original list.

If there are duplicate elements in the traversable object, the set() function will delete the duplicate data. For example:

characters = set('letter')
print(characters)

The output results are as follows:

{'r', 'l', 't', 'e'}

The word "letter" in the above example contains two e and two t letters, and set() removes a duplicate value respectively.

Gets the size of the collection

The built-in len() function can be used to return the number of elements in the collection.

len(set)

For example:

ratings = {1, 2, 3, 4, 5}
size = len(ratings)

print(size)    

The output results are as follows:

5

Check the existence of elements in the collection

If you want to determine whether an element exists in the set, you can use the in operator:

element in set

If the specified element exists in the collection, the in operator will return True; Otherwise, False is returned. For example:

ratings = {1, 2, 3, 4, 5}
rating = 1

if rating in ratings:
    print(f'The set contains {rating}')

The output results are as follows:

The set contains 1

By adding not before the in operator, you can perform the opposite judgment. For example:

ratings = {1, 2, 3, 4, 5}
rating = 6

if rating not in ratings:
    print(f'The set does not contain {rating}')

The output results are as follows:

The set does not contain 6

Add elements to the collection

If you want to add an element to the collection, you can use the add() method:

set.add(element)

For example:

skills = {'Python programming', 'Software design'}
skills.add('Problem solving')

print(skills)

The output results are as follows:

{'Problem solving', 'Software design', 'Python programming'}

Deletes the specified element from the collection

If you want to delete an element from the collection, you can use the remove() method:

set.remove(element)

For example:

skills = {'Problem solving', 'Software design', 'Python programming'}
skills.remove('Software design')

print(skills)

The output results are as follows:

{'Problem solving', 'Python programming'}

An error message will be returned if the element does not exist. For example:

skills = {'Problem solving', 'Software design', 'Python programming'}
skills.remove('Java')

The error message is as follows:

KeyError: 'Java'

To avoid the above errors, we can use the in operator to check whether the element exists before deleting the element:

skills = {'Problem solving', 'Software design', 'Python programming'}
if 'Java' in skills:
    skills.remove('Java')

For ease of use, the collection provides a discard() method to delete elements. When the deleted element does not exist, the method will not return an error:

set.discard(element)

For example:

skills = {'Problem solving', 'Software design', 'Python programming'}
skills.discard('Java')

Returns the specified element in the collection

If you want to delete and return an element in the collection, you can use the pop() method. Since the elements in the collection have no order, the pop() method randomly deletes an element from the collection.

If we execute the following code several times, different results will be displayed each time:

skills = {'Problem solving', 'Software design', 'Python programming'}
skill = skills.pop()

print(skill)

Delete all elements in the collection

If you want to delete all the elements in the collection, you can use the clear() method:

set.clear()

For example:

skills = {'Problem solving', 'Software design', 'Python programming'}
skills.clear()

print(skills)

The output results are as follows:

set()

Freeze collection

The built-in function frozenset() can set the set to immutable, which creates a new immutable set based on the existing set. For example:

skills = {'Problem solving', 'Software design', 'Python programming'}
skills = frozenset(skills)

After that, if we try to modify the elements of the collection, we will return an error:

skills.add('Django')
AttributeError: 'frozenset' object has no attribute 'add'

Traverse the elements in the collection

A collection is a kind of traversable object. We can use the for loop to traverse the elements in the collection. For example:

skills = {'Problem solving', 'Software design', 'Python programming'}

for skill in skills:
    print(skill)

The output results are as follows:

Software design
Python programming
Problem solving

If you want to access the subscript of the current element inside the loop, you can use the built-in enumerate() function:

skills = {'Problem solving', 'Software design', 'Python programming'}

for index, skill in enumerate(skills):
    print(f"{index}.{skill}")

The output results are as follows:

0.Software design
1.Python programming
2.Problem solving

By default, the subscript of an element starts at 0. If you want to change this behavior, you can specify a second parameter for the enumerate() function. For example:

skills = {'Problem solving', 'Software design', 'Python programming'}

for index, skill in enumerate(skills, 1):
    print(f"{index}.{skill}")

The output results are as follows:

1.Python programming
2.Problem solving
3.Software design

Each time the above code is executed, the collection elements are returned in a different order.

In the next tutorial, we will show you how to perform various collection operations. For example: Union, intersection, difference and symmetric difference of sets.

summary

  • A collection is an unordered list of immutable objects.

Topics: Python set