Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sets | Data Structures
Introduction to Python with AI

bookSets

A set is a collection of unique, unordered values. It doesn't allow duplicates and doesn't keep order.

Sets are useful for removing repeats, checking membership, or comparing groups with operations like union and intersection.

Note
Note
  • How do you define a set in Python? Give code example with curly braces and set function both.
  • What makes sets different from lists in Python? Give code example that shows these differences.
  • How do you add and remove items in a set? Give code example using add, remove and discard.
  • What are basic set operations in Python? Give code example for union, intersection, and difference.

Creating a Set

Create a set with curly braces, without key-value pairs: fruits = {'apple', 'banana', 'cherry'}

Or use set() to convert another collection, like a list, which also removes duplicates automatically.

Key Properties of Sets

  • Unordered: elements have no fixed order;
  • No duplicates: repeated items are ignored;
  • Mutable: you can add or remove items;
  • Immutable items only: allowed types include numbers, strings, tuples;
  • No indexing: elements can't be accessed by position.

Sets are optimized for fast membership tests with the in keyword.

Adding and Removing Items

  • .add(): to insert a new item into a set;
  • .remove(): to remove an item, it raises an error if the item doesn't exist;
  • .discard(): also removes item, but quietly skips if the item's not found.

Set Operations

Python sets support:

  • Union (| or .union()): combine elements from both sets;
  • Intersection (& or .intersection()): keep only common elements;
  • Difference (- or .difference()): elements in one set but not the other.

These operations are handy for comparing roles, flags, or datasets.

Summary

  • Sets are unordered collections of unique values;
  • They automatically remove duplicates;
  • You can add or remove items, but can't access by position;
  • Use sets for fast comparisons, membership checks, and when you don't care about order.

Try It Yourself

  1. Create two sets:
    • One with "apple", "banana", "cherry";
    • Another with "banana", "kiwi", "grape".
  2. Print their union, intersection, and difference;
  3. Then add "grape" to the first set, and discard "kiwi" from the second;
  4. Print their union, intersection, and difference again.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookSets

Swipe to show menu

A set is a collection of unique, unordered values. It doesn't allow duplicates and doesn't keep order.

Sets are useful for removing repeats, checking membership, or comparing groups with operations like union and intersection.

Note
Note
  • How do you define a set in Python? Give code example with curly braces and set function both.
  • What makes sets different from lists in Python? Give code example that shows these differences.
  • How do you add and remove items in a set? Give code example using add, remove and discard.
  • What are basic set operations in Python? Give code example for union, intersection, and difference.

Creating a Set

Create a set with curly braces, without key-value pairs: fruits = {'apple', 'banana', 'cherry'}

Or use set() to convert another collection, like a list, which also removes duplicates automatically.

Key Properties of Sets

  • Unordered: elements have no fixed order;
  • No duplicates: repeated items are ignored;
  • Mutable: you can add or remove items;
  • Immutable items only: allowed types include numbers, strings, tuples;
  • No indexing: elements can't be accessed by position.

Sets are optimized for fast membership tests with the in keyword.

Adding and Removing Items

  • .add(): to insert a new item into a set;
  • .remove(): to remove an item, it raises an error if the item doesn't exist;
  • .discard(): also removes item, but quietly skips if the item's not found.

Set Operations

Python sets support:

  • Union (| or .union()): combine elements from both sets;
  • Intersection (& or .intersection()): keep only common elements;
  • Difference (- or .difference()): elements in one set but not the other.

These operations are handy for comparing roles, flags, or datasets.

Summary

  • Sets are unordered collections of unique values;
  • They automatically remove duplicates;
  • You can add or remove items, but can't access by position;
  • Use sets for fast comparisons, membership checks, and when you don't care about order.

Try It Yourself

  1. Create two sets:
    • One with "apple", "banana", "cherry";
    • Another with "banana", "kiwi", "grape".
  2. Print their union, intersection, and difference;
  3. Then add "grape" to the first set, and discard "kiwi" from the second;
  4. Print their union, intersection, and difference again.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
some-alt