0% found this document useful (0 votes)
8 views14 pages

Work Left To Do

The document provides a Python program that demonstrates various inbuilt functions for lists and dictionaries. It includes a menu-driven interface for performing operations such as adding, removing, and accessing elements in both data structures. The program allows users to interactively manage a dictionary and a list, showcasing the functionalities available in Python.

Uploaded by

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

Work Left To Do

The document provides a Python program that demonstrates various inbuilt functions for lists and dictionaries. It includes a menu-driven interface for performing operations such as adding, removing, and accessing elements in both data structures. The program allows users to interactively manage a dictionary and a list, showcasing the functionalities available in Python.

Uploaded by

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

17.

Write a program to demonstrate all types


of list and
dictionary inbuilt functions in python.

#Program to demonstrate all type of


dictionary inbuilt
functions

def display_menu():
print("\nDictionary Operations Menu:")
print("1. Add a key-value pair")
print("2. Remove a key")
print("3. Get value by key")
print("4. Check if key exists")
print("5. Get all keys")
print("6. Get all values")
print("7. Get all items")
print("8. Clear dictionary")
print("9. Exit")

def add_key_value(dictionary):
while True:
key = input("Enter key (or 'done' to finish):
")
if key == "done":
break

value = input("Enter value for " + key + ": ")


dictionary[key] = value
print("Displaying the dictionary:")
for j, (key, value) in
enumerate(dictionary.items()):
print(f"{key}: {value}", end=' ')
return dictionary

def remove_key(dictionary):
key = input("Enter key to remove: ")
if key in dictionary:
del dictionary[key]
print(f"Removed key '{key}' from the
dictionary.")
else:
print(f"Key '{key}' not found in the
dictionary.")

def get_value(dictionary):
key = input("Enter key to get value: ")
if key in dictionary:
print(f"Value for key '{key}':
{dictionary[key]}")
else:
print(f"Key '{key}' not found in the
dictionary.")

def check_key_exists(dictionary):
key = input("Enter key to check: ")
if key in dictionary:
print(f"Key '{key}' exists in the
dictionary.")
else:
print(f"Key '{key}' does not exist in the
dictionary.")

def get_all_keys(dictionary):
print("All keys in the dictionary:",
list(dictionary.keys()))

def get_all_values(dictionary):
print("All values in the dictionary:",
list(dictionary.values()))

def get_all_items(dictionary):
print("All items in the dictionary:",
list(dictionary.items()))

def clear_dictionary(dictionary):
dictionary.clear()
print("Dictionary cleared.")

def main():
dictionary = {}
while True:
display_menu()
choice = input("Enter your choice (1-9): ")
if choice == '1':
add_key_value(dictionary)
elif choice == '2':
remove_key(dictionary)
elif choice == '3':
get_value(dictionary)
elif choice == '4':
check_key_exists(dictionary)
elif choice == '5':
get_all_keys(dictionary)
elif choice == '6':
get_all_values(dictionary)
elif choice == '7':
get_all_items(dictionary)
elif choice == '8':
clear_dictionary(dictionary)

elif choice == '9':


print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()
#Program to demonstrate all type of list inbuilt
functions

def create_list():
my_list = []
num_elements = int(input("Enter the number
of elements in your list: "))
print("Enter elements one by one:")
for _ in range(num_elements):
element = input()
my_list.append(element)
return my_list

def print_list(my_list):
print("Your list:", my_list)

def access_element(my_list):
index = int(input("Enter the index of the
element you want to access: "))
if 0 <= index < len(my_list):
print("Element at index", index, ":",
my_list[index])
else:
print("Invalid index.")

def add_element(my_list):
new_element = input("Enter the element to
add: ")
my_list.append(new_element)
print("Element added successfully.")

def insert_element(my_list):
index = int(input("Enter the index where you
want to insert: "))
new_element = input("Enter the element to
insert: ")
if 0 <= index <= len(my_list):
my_list.insert(index, new_element)
print("Element inserted successfully.")
else:
print("Invalid index.")

def remove_element(my_list):
element_to_remove = input("Enter the
element to remove: ")
if element_to_remove in my_list:
my_list.remove(element_to_remove)
print("Element removed successfully.")
else:
print("Element not found in the list.")

def update_element(my_list):
index = int(input("Enter the index of the
element to update: "))
if 0 <= index < len(my_list):
new_value = input("Enter the new value: ")
my_list[index] = new_value
print("Element updated successfully.")
else:
print("Invalid index.")

def main():
my_list = create_list()

while True:
print("\nChoose an operation:")
print("1. Print list")
print("2. Access element")
print("3. Add element")
print("4. Insert element")
print("5. Remove element")
print("6. Update element")
print("7. Exit")
choice = input("Enter your choice: ")

if choice == '1':
print_list(my_list)
elif choice == '2':
access_element(my_list)
elif choice == '3':
add_element(my_list)
elif choice == '4':
insert_element(my_list)
elif choice == '5':
remove_element(my_list)
elif choice == '6':
update_element(my_list)
elif choice == '7':
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()

OUTPUT:

Dictionary Operations Menu:


1. Add a key-value pair
2. Remove a key
3. Get value by key
4. Check if key exists
5. Get all keys
6. Get all values
7. Get all items
8. Clear dictionary
9. Exit
Enter your choice (1-9):
1
Enter key (or 'done' to finish):
1
Enter value for 1:
Time
Enter key (or 'done' to finish):
2
Enter value for 2:
Power
Enter key (or 'done' to finish):
3

Enter value for 3:


Divine
Enter key (or 'done' to finish):
4
Enter value for 4:
Titans
Enter key (or 'done' to finish):
5
Enter value for 5:
Mythic
Enter key (or 'done' to finish):
done
Displaying the dictionary:
1: Time 2: Power 3: Divine 4: Titans 5: Mythic

Dictionary Operations Menu:


1. Add a key-value pair
2. Remove a key
3. Get value by key
4. Check if key exists
5. Get all keys
6. Get all values
7. Get all items
8. Clear dictionary

9. Exit

Enter your choice (1-9):


9
Exiting program.

OUTPUT:

Enter the number of elements in your list: 3


Enter elements one by one:
apple
banana
cherry

Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element

7. Exit
Enter your choice: 1
Your list: ['apple', 'banana', 'cherry']

Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Enter your choice: 3
Enter the element to add: date
Element added successfully.

Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit

Enter your choice: 1


Your list: ['apple', 'banana', 'cherry', 'date']

Choose an operation:
1. Print list
2. Access element
3. Add element
4. Insert element
5. Remove element
6. Update element
7. Exit
Enter your choice:7

You might also like