0% found this document useful (0 votes)
10 views7 pages

EXP 6 (Programs)

The document outlines various Python programming tasks focused on sets and dictionaries, including checking for element presence, finding unique words, separating names, merging dictionaries, sorting dictionaries by key, and removing specific keys. Each task includes an aim, algorithm, and example program code. The programs demonstrate practical applications of set and dictionary operations in Python.

Uploaded by

nobisuki005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

EXP 6 (Programs)

The document outlines various Python programming tasks focused on sets and dictionaries, including checking for element presence, finding unique words, separating names, merging dictionaries, sorting dictionaries by key, and removing specific keys. Each task includes an aim, algorithm, and example program code. The programs demonstrate practical applications of set and dictionary operations in Python.

Uploaded by

nobisuki005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXP NO: 6

PYTHON PROGRAMS ON SETS AND DICTIONARIES


DATE: 17.03.2025

AIM:

To write programs in Python which operates on Sets and Dictionaries

AIM-6A:

To write a program to check if a given value is present in a set or not, if two given sets
have no elements in common, if a given set is a superset of itself and a superset of another
given set, to find elements in a given set that are not in another set.

ALGORITHM:

➢ Get input for set1 and convert it into a set.


➢ Get an element from the user and check if it's in set1.
➢ Get input for set2 and convert it into a set.
➢ Find and display common elements between set1 and set2.
➢ Check if set1 is a superset of set2 and vice-versa.
➢ Find and display elements in set1 that are not in set2.

PROGRAM:

set1=set(input("Enter elements for set1 (comma-separated): ").split(","))

element=input("Enter an element to find in set1: ")

print("Element found" if element in set1 else "Element not found")

set2=set(input("Enter elements for set2 (comma-separated): ").split(","))

common_elements = set1 & set2

if common_elements:

print("Common Elements: ",common_elements)

else:

print("No common elements")

if set1.issuperset(set2):

print("Set1 is a superset of set2")


else:

print("Set1 is not a superset of set2")

if set2.issuperset(set1):

print("Set2 is a superset of set1")

else:

print("Set2 is not a superset of set1")

print("Elements in set1 that are not in set2: ",set1 - set2)


AIM-6B:

To write a program to find all the unique words and count the frequency of occurrence
from a given list of strings using python set data type.

ALGORITHM:

➢ Take a comma-separated list of words as input and split the input into a list.
➢ Remove duplicates by converting the list to a set.
➢ Convert the set back to a list.
➢ For each word in the list count its occurrences in the original list and print the word
and its count.

PROGRAM:

words_list = input("Enter a list of words (comma-separated): ").split(",")

unique_words = set(words_list)

for word in new_lst:5

print(f"{word.strip()}: {words_list.count(word)}")
AIM-6C:

To write a program to separate out the names into two sets, one containing names
beginning with A and another containing names beginning with B.

ALGORITHM:

➢ Get a comma-separated list of names from the user.


➢ Remove duplicates by converting the list to a set and convert the set back to list.
➢ Create two empty lists for names starting with 'A' and 'B'.
➢ Check each name, if it starts with 'A' or 'a', add it to the first list or If it starts with 'B'
or 'b', add it to the second list.
➢ Convert both lists to sets and Print the names starting with 'A' and 'B'.

PROGRAM:

names_list = input("Enter a list of names (comma-separated): ").split(",")

set1 = set(names_list)

lst = list(set1)

lstA = []

lstB = []

for name in lst:

name = name.strip()

if name.startswith(('A', 'a')):

lstA.append(name)

elif name.startswith(('B', 'b')):

lstB.append(name)

setA = set(lstA)

setB = set(lstB)

print("Names starting with 'A':", setA)

print("Names starting with 'B':", setB)


AIM-6D:

To write a function to combine two dictionaries by adding values for common keys. If a
key exists in both dictionaries, the values should be added together.

ALGORITHM:

➢ Define two dictionaries with keys and values.


➢ Copy all elements from dict1 into a new dictionary merged_dict.
➢ Iterate through each key-value pair in dict2:
➢ If the key exists in merged_dict, add its value to the existing value.
➢ Else, add the key-value pair to merged_dict and return the merged_dict

PROGRAM:

def merge_dictionaries(dict1, dict2):

merged_dict = dict1.copy()

for key, value in dict2.items():

if key in merged_dict:

merged_dict[key] += value

else:

merged_dict[key] = value

return merged_dict

dict1 = {'a': 10, 'b': 20, 'c': 30}

dict2 = {'b': 15, 'c': 25, 'd': 40}

result = merge_dictionaries(dict1, dict2)

print(result)
AIM-6E:

To write a Python program to sort a given dictionary by key.

ALGORITHM:

➢ Accept or define the dictionary that needs to be sorted.


➢ Use sorted() on the dictionary's items (input_dict.items())to sort them by key.
➢ Convert the sorted list of tuples back into a dictionary using dict().
➢ Return the dictionary sorted by key.
PROGRAM:

def get_dictionary():

n = int(input("Enter the number of elements in the dictionary: "))

user_dict = {}

for _ in range(n):

key = input("Enter key: ")

value = input("Enter value: ")

user_dict[key] = value

return user_dict

def sort_dictionary_by_key(input_dict):

sorted_dict = dict(sorted(input_dict.items()))

return sorted_dict

print("Enter dictionary values:")

user_dict = get_dictionary()

sorted_result = sort_dictionary_by_key(user_dict)

print("Sorted Dictionary:", sorted_result)


AIM-6F:

To implement a function that removes a specific key from a dictionary and returns the
modified dictionary

ALGORITHM:

➢ Accept or define the dictionary and the key you want to remove.
➢ Check if the Key Exists:
If the key exists in the dictionary, remove it using del or pop().
If the key does not exist, print a message saying the key is not found.
➢ Return the Modified Dictionary.

PROGRAM:

def remove_key_from_dict(input_dict, key_to_remove):

if key_to_remove in input_dict:

del input_dict[key_to_remove]

print(f"Key '{key_to_remove}' removed successfully.")

else:

print(f"Key '{key_to_remove}' not found in the dictionary.")

return input_dict

n = int(input("Enter the number of elements in the dictionary: "))

user_dict = {}

for _ in range(n):

key = input("Enter key: ")

value = input("Enter value: ")

user_dict[key] = value

key_to_remove = input("Enter the key to remove: ")

updated_dict = remove_key_from_dict(user_dict, key_to_remove)

print("Updated Dictionary:", updated_dict)

You might also like