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

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.