In the previous article, we have discussed Python Program for Set pop() Method
Dictionary in python :
A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.
Examples:
Example1:
Input:
Given dictionary = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}
Output:
The above given dictionary is : {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150} The above given dictionary keys are : dict_keys(['hello', 'this', 'is', 'btechgeeks']) The above given new dictionary is : {} The above given New empty dictionary keys are : dict_keys([])
Example2:
Input:
Given dictionary = {10: 'good', 20: 'morning', 30: 'btechgeeks'}
Output:
The above given dictionary is : {10: 'good', 20: 'morning', 30: 'btechgeeks'} The above given dictionary keys are : dict_keys([10, 20, 30]) The above given new dictionary is : {} The above given New empty dictionary keys are : dict_keys([])
Program for Dictionary keys() Function in Python
Dictionary keys() Function:
In python, the dictionary keys() function returns a list of available keys in a dictionary (total keys present).
Syntax:
dictionary.keys()
Parameter Values: The dictionary keys() function doesn’t have any parameters.
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the dictionary as static input by initializing it with some random values and store it in a variable.
- Print the above-given dictionary.
- Apply dictionary. keys() method to the above-given dictionary to get all the keys.
- Store it in another variable.
- Print the above-given dictionary keys.
- Take a new empty dictionary and store it in another variable.
- Print the above-initialized new empty dictionary.
- Apply dictionary. keys() method to the above new empty dictionary and print it.
- The Exit of Program.
Below is the implementation:
# Give the dictionary by initializing it with some random values and store it # in a variable. gvn_dictinry = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150} # Print the above-given dictionary. print("The above given dictionary is :") print(gvn_dictinry) # Apply dictionary.keys() method to the above-given dictionary to get all the keys. # Store it in another variable dict_keyss = gvn_dictinry.keys() # Print the above given dictionary keys. print("The above given dictionary keys are :") print(dict_keyss) # Take a new empty dictionary and store it in another variable. new_dict = {} # Print the above-initialized new empty dictionary. print("The above given new dictionary is :") print(new_dict) # Apply dictionary.keys() method to the above new empty dictionary and print it. print("The above given New empty dictionary keys are :") print(new_dict.keys())
Output:
The above given dictionary is : {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150} The above given dictionary keys are : dict_keys(['hello', 'this', 'is', 'btechgeeks']) The above given new dictionary is : {} The above given New empty dictionary keys are : dict_keys([])
Method #2: Using Built-in Functions (User Input)
Approach:
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Print the above-given dictionary.
- Apply dictionary. keys() method to the above-given dictionary to get all the keys.
- Store it in another variable.
- Print the above-given dictionary keys.
- Take a new empty dictionary and store it in another variable.
- Print the above-initialized new empty dictionary.
- Apply dictionary. keys() method to the above new empty dictionary and print it.
- The Exit of Program.
Below is the implementation:
# Take a dictionary and initialize it with an empty dictionary using dict() or {}. gvn_dictinry = {} # Give the number of keys as user input using int(input()) and store it in a variable. numb_of_kys = int( input('Enter some random number of keys of the dictionary = ')) # Loop till the given number of keys using for loop. for p in range(numb_of_kys): # Inside the for loop scan the key and value as # user input using input(),split() functions # and store them in two separate variables. keyy, valuee = input( 'Enter key and value separated by spaces = ').split() # Initialize the key with the value of the dictionary. gvn_dictinry[keyy] = valuee # Print the above-given dictionary. print("The above given dictionary is :") print(gvn_dictinry) # Apply dictionary.keys() method to the above-given dictionary to get all the keys. # Store it in another variable dict_keyss = gvn_dictinry.keys() # Print the above given dictionary keys. print("The above given dictionary keys are :") print(dict_keyss) # Take a new empty dictionary and store it in another variable. new_dict = {} # Print the above-initialized new empty dictionary. print("The above given new dictionary is :") print(new_dict) # Apply dictionary.keys() method to the above new empty dictionary and print it. print("The above given New empty dictionary keys are :") print(new_dict.keys())
Output:
Enter some random number of keys of the dictionary = 3 Enter key and value separated by spaces = 10 good Enter key and value separated by spaces = 20 morning Enter key and value separated by spaces = 30 btechgeeks The above given dictionary is : {'10': 'good', '20': 'morning', '30': 'btechgeeks'} The above given dictionary keys are : dict_keys(['10', '20', '30']) The above given new dictionary is : {} The above given New empty dictionary keys are : dict_keys([])
Want to excel in creating, adding, removing elements from python dictionaries? Look at this tutorial ie., python dictionary method examples, and explore more.