Python Sets

Buildandteach: Henry Palma
1 min readFeb 17, 2022

--

Features of a Set

A set is a collection of objects. It contains the following features:

  • It is unordered
  • The items in the set are unchangeable but the set is changeable
  • It is unindexed
  • Doesn’t allow duplicates

1) Create a Set

2) Loop Through a Set

Use a for loop to loop through a set.

3) Add a item to a Set

Use the add method to add an item to a set.

4) Remove an item from a Set

To remove an item from a Set use the remove() method

5) Set Operations: Combine 2 Sets. Keep only unique values

Use the “|” operator to combine 2 sets and keep only unique values

6) Set Operations: Combine 2 Sets and keep only common values

Use the “&” operator to combine 2 sets and keep only common values.

7) Set Operations: Combine 2 Sets and remove items in the first list that are present in the second list

Use the “-” operator to combine 2 Sets and remove items in the first list that are present in the second list

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response