PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python Exercises » Python Set Exercise with Solutions

Python Set Exercise with Solutions

Updated on: October 20, 2022 | 24 Comments

Set in Python is an unordered collection of items. Every item is unique in it. i.e., the set doesn’t allow duplicates.

This Python set exercise aims to help you to learn and practice set operations. All questions are tested on Python 3.

Also Read:

  • Python Sets
  • Python Set Quiz

This Python set exercise includes the following: –

  • It contains 10 questions on set operations, manipulations, and set functions.
  • This coding exercise includes set assignments, programs and challenges.

When you complete each question, you get more familiar with the Python set. Let us know if you have any alternative solutions. It will help other developers.

  • Use Online Code Editor to solve exercise questions.
  • Read the complete guide on Python Sets to solve this exercise.

Table of contents

  • Exercise 1: Add a list of elements to a set
  • Exercise 2: Return a new set of identical items from two sets
  • Exercise 3: Get Only unique items from two sets
  • Exercise 4: Update the first set with items that don’t exist in the second set
  • Exercise 5: Remove items from the set at once
  • Exercise 6: Return a set of elements present in Set A or B, but not both
  • Exercise 7: Check if two sets have any elements in common. If yes, display the common elements
  • Exercise 8: Update set1 by adding items from set2, except common items
  • Exercise 9: Remove items from set1 that are not common to both set1 and set2

Exercise 1: Add a list of elements to a set

Given a Python list, Write a program to add all its elements into a given set.

Given:

sample_set = {"Yellow", "Orange", "Black"}
sample_list = ["Blue", "Green", "Red"]

Expected output:

Note: Set is unordered.

{'Green', 'Yellow', 'Black', 'Orange', 'Red', 'Blue'}
Show Hint

Use the update() method of a set.

Show Solution
sample_set = {"Yellow", "Orange", "Black"}
sample_list = ["Blue", "Green", "Red"]

sample_set.update(sample_list)
print(sample_set)

Exercise 2: Return a new set of identical items from two sets

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{40, 50, 30}
Show Hint

Use the intersection() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

print(set1.intersection(set2))

Exercise 3: Get Only unique items from two sets

Write a Python program to return a new set with unique items from both sets by removing duplicates.

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{70, 40, 10, 50, 20, 60, 30}

Note: set is unordered, so not necessary this will be the order of the item.

Show Hint

Use the union() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

print(set1.union(set2))

Exercise 4: Update the first set with items that don’t exist in the second set

Given two Python sets, write a Python program to update the first set with items that exist only in the first set and not in the second set.

Given:

set1 = {10, 20, 30}
set2 = {20, 40, 50}

Expected output:

set1 {10, 30}
Show Hint

Use the difference_update() method of a set.

Show Solution
set1 = {10, 20, 30}
set2 = {20, 40, 50}

set1.difference_update(set2)
print(set1)

Exercise 5: Remove items from the set at once

Write a Python program to remove items 10, 20, 30 from the following set at once.

Given:

set1 = {10, 20, 30, 40, 50}

Expected output:

{40, 50}
Show Hint

Use the difference_update() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set1.difference_update({10, 20, 30})
print(set1)

Exercise 6: Return a set of elements present in Set A or B, but not both

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{20, 70, 10, 60}
Show Hint

Use the symmetric_difference() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

print(set1.symmetric_difference(set2))

Exercise 7: Check if two sets have any elements in common. If yes, display the common elements

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {60, 70, 80, 90, 10}

Expected output:

Two sets have items in common
{10}
Show Hint
  • Use the isdisjoint() method check if sets has a common elements
  • If above condition is true then use the intersection() method to display common elements
Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {60, 70, 80, 90, 10}

if set1.isdisjoint(set2):
  print("Two sets have no items in common")
else:
  print("Two sets have items in common")
  print(set1.intersection(set2))

Exercise 8: Update set1 by adding items from set2, except common items

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{70, 10, 20, 60}
Show Hint

Use the symmetric_difference_update() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

set1.symmetric_difference_update(set2)
print(set1)

Exercise 9: Remove items from set1 that are not common to both set1 and set2

Given:

set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

Expected output:

{40, 50, 30}
Show Hint

Use the intersection_update() method of a set.

Show Solution
set1 = {10, 20, 30, 40, 50}
set2 = {30, 40, 50, 60, 70}

set1.intersection_update(set2)
print(set1)

Filed Under: Python, Python Basics, Python Exercises

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python Basics Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python Basics Python Exercises
TweetF  sharein  shareP  Pin

 Python Exercises

  • Python Exercises Home
  • Basic Exercise for Beginners
  • Input and Output Exercise
  • Loop Exercise
  • Functions Exercise
  • String Exercise
  • Data Structure Exercise
  • List Exercise
  • Dictionary Exercise
  • Set Exercise
  • Tuple Exercise
  • Date and Time Exercise
  • OOP Exercise
  • Python JSON Exercise
  • Random Data Generation Exercise
  • NumPy Exercise
  • Pandas Exercise
  • Matplotlib Exercise
  • Python Database Exercise

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com