Function to Find and Remove Duplicates from dictionaries

Python Practice question

Function to Find and Remove Duplicates from dictionaries

Instructions

The aim of this Python practice question is to create a function that takes a dictionary as argument and returns the same dictionary without duplicates along with the number of unique values in the dictionary.

The dictionary “passengers” contains the passengers of a regional flight. It includes their names and IDs issued with the ticket. The problem is that there are duplicates and you need to know the distinct passengers and how many of them are going to be onboard.
Write a function that takes “passengers” and prints the unique traverlers and how many are there.

passengers = { ‘ZJY9666’: ‘Jordan Doyle’, ‘WIL9666’: ‘Fletcher Willis’, ‘JRT9666’: ‘Cade Ortiz’,
‘FSL9666’: ‘Marian Fultone’, ‘MYT9666’: ‘Camilo Mat’, ‘ZJY9661’: ‘John Roy’,
‘ZJT9666’: ‘Ashley Arpey’, ‘LYW9666’: ‘Laurent Bacall’,
‘JSM9666’: ‘Angela Justic’, ‘BYX9666’: ‘Armando Baxtero’,
‘BLY9666’: ‘Orson Wells’, ‘BSL9666’: ‘Bullock Floor’,
‘MPX9666’: ‘Loneghan Sexton’, ‘ZJT9661’: ‘David Peto’,
‘ZPJ9666’: ‘Jason Dejesus’, ‘XJF9666’: ‘Herman Cortes’,
‘BYX9661’: ‘Laura Bardot’, ‘SDR0000’: ‘Davide Jon’ ,
‘ZJT9666’: ‘Ashley Arpey’, ‘LYW9666’: ‘Laurent Bacall’,
‘Camilo Mat’, ‘ZJY9661’: ‘John Roy’,’BLY9666′: ‘Orson Wells’,
‘BSL9666’: ‘Bullock Floor’, ‘ZPJ9666’: ‘Jason Dejesus’, ‘XJF9666’: ‘Herman Cortes’,
‘ZJY9666’: ‘Jordan Doyle’, ‘WIL9666’: ‘Fletcher Willis’,
‘Angela Justic’, ‘BYX9666’: ‘Armando Baxtero’}

Guidelines

  • Create an empty dictionary called "unique_passengers" to store the unique passengers.
  • Iterate over the items in the "passengers" dictionary using a for loop.
  • For each passenger, check if their name (passenger_name) is already present in the values of "unique_passengers" using the "not in" operator.
  • If the passenger name is not present, add the passenger ID (passenger_id) as the key and the passenger name as the value to the "unique_passengers" dictionary.
  • Finally, return the "unique_passengers" dictionary and the length of the dictionary using the "len()" function.

Solutions

## Define the function
def remove_duplicates(passengers):
    unique_passengers = {}
    for passenger_id, passenger_name in passengers.items():
        if passenger_name not in unique_passengers.values():
            unique_passengers[passenger_id] = passenger_name
    return unique_passengers, len(unique_passengers)

Expected output

remove_duplicates(passengers)

{'ZJY9666': 'Jordan Doyle',
  'WIL9666': 'Fletcher Willis',
  'JRT9666': 'Cade Ortiz',
  'FSL9666': 'Marian Fultone',
  'MYT9666': 'Camilo Mat',
  'ZJT9666': 'Ashley Arpey',
  'LYW9666': 'Laurent Bacall',
  'JSM9666': 'Angela Justic',
  'BYX9666': 'Armando Baxtero',
  'BLY9666': 'Orson Wells',
  'BSL9666': 'Bullock Floor',
  'BST0000': 'Valentino Popino',
  'MPX9666': 'Loneghan Sexton',
  'ZJT9661': 'David Peto',
  'ZPJ9666': 'Jason Dejesus',
  'XJF9666': 'Herman Cortes',
  'BYX9661': 'Laura Bardot',
  'SDR0000': 'Davide Jon'},
 18)

 

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart