Python | Working with sets exercise | Use sets to remove duplicates and compare two lists

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Python  (28 exercises)
Version ==> Latest version
Topic ==> Working with sets  (1 exercise)
Level ==> Average difficulty
Subject ==> Python training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Open the file in the above folder and update the file path to point to the above folder too:

File path

You'll need to change this file path for this program to work.

Run the program - it should print out two lists of grand prix (one for 2016, and the other for 2017):

Part of output

A part of the output from the program. Each list contains duplicates, and there are discrepancies between the two lists.


Create variables holding the two lists as sets instead (this will, miraculously, automatically remove any duplicates), and use the correct set operator to list out all the elements in one set but not in both:

Listing discrepancies

You'll need to use an if condition to determine which list each element belonged to so that you can include the year in each message.

When you've finished, take a moment to reflect on just how few lines of code that took, then close your file down.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post