Register to get access to free programming courses with interactive exercises

Methods of set objects Python: Dictionaries and Sets

Making operations on sets as methods

You've already learned about operators that allow you to combine sets. These operators are as similar as possible to those used in set theory in mathematics. Every programmer should at least know the basics of set theory. For this reason, we should use sets in combination with operators.

However, we should mention that each operator has a verbal equivalent method:

a.union(b)                 # equivalent "a | b"
a.intersection(b)          # equivalent "a & b"
a.difference(b)            # equivalent "a - b"
a.symmetric_difference(b)  # equivalent "a ^ b"

Updating of sets in place

There is another reason why we're talking about the four methods above. Remember, we looked at the dictionary update method, updating the dictionary locally using data from another dictionary? There are several of these sorts of update methods.

The difference_update method

The difference_update works similarly to - or difference. It removes from the set all elements that are in the set argument of the associated one:

a, b = {1, 2}, {2, 3}
a.difference_update(b)
a  # {1}

The intersection_update method

The intersection_update is the modifying analog of & or intersection. It leaves only those elements in the linked set that are in the argument set:

a, b = {1, 2}, {2, 3}
a.intersection_update(b)
a  # {2}

The symmetric_difference_update method

The symmetric_difference_update is the modifying analog of ^ or symmetric_difference. It adds elements to the linked set that are only in the argument and removes elements that are in both sets:

a, b = {1, 2}, {2, 3}
a.symmetric_difference_update(b)
a  # {1, 3}

The update method

The update is the modifying equivalent of | or union. It complements the associated set with missing elements from the argument set:

a, b = {1, 2}, {2, 3}
a.update(b)
a  # {1, 2, 3}

Concerning uniformity, the update should have been called union_update. But we chose the more common name update because developers often use this name for similar methods in other collections.


Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.