Q:

Python program to create a dictionary with mixed keys, and print the keys, values & key-value pairs

belongs to collection: Python Dictionary Programs

0

dictionary contains the pair of the keys & values (represents key : value), a dictionary is created by providing the elements within the curly braces ({}), separated by the commas. 

Here, we are creating a dictionary with mixed keys and printing the dictionary, it’s keys, it’s values and key-value pairs.

  • Printing the dictionary – To print a dictionary, just pass the dictionary_name in the print function.
    Syntax:
    print(dictionary_name)
    
  • Printing the keys – To print the dictionary keys only, loop through the dictionary and print it, when we loop through a dictionary it returns the keys.
    Syntax:
    for key in dictionary_name:
        print(key)
    
  • Printing the values – To print the dictionary values only, loop through the dictionary and use the values() method, it returns the dictionary values.
    Syntax:
    for value in dictionary_name.values():
        print(value)
    
  • Print the key-value pairs – To print the keys & values both, loop through the dictionary and use the items() method, it returns the key-value pair.
    Syntax:
    for key,value in dictionary_name.items():
        print(key, value)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# Python program to create a dictionary 
# with mixed keys

# creating the dictionary
dict_a = {1 : "India", 'city' : 'New Delhi', 
'Record' : {'id' : 101, 'name' : 'Amit', 'age': 21}}

# printing the dictionary
print("Dictionary \'dict_a\' is...")
print(dict_a)

# printing the keys only
print("Dictionary \'dict_a\' keys...")
for x in dict_a:
    print(x)

# printing the values only
print("Dictionary \'dict_a\' values...")
for x in dict_a.values():
    print(x)

# printing the keys & values
print("Dictionary \'dict_a\' keys & values...")
for x, y in dict_a.items():
    print(x, ':', y)

# printing the keys & values of Sub-Dictionary (Record)
print("Sub-Dictionary \'Record\' keys & values...")
for x, y in dict_a['Record'].items():
    print(x, ':', y)

Output:

Dictionary 'dict_a' is...
{1: 'India', 'Record': {'id': 101, 'age': 21, 'name': 'Amit'}, 'city': 'New Delhi'}
Dictionary 'dict_a' keys...
1  
Record
city  
Dictionary 'dict_a' values... 
India 
{'id': 101, 'age': 21, 'name': 'Amit'} 
New Delhi
Dictionary 'dict_a' keys & values...
1 : India
Record : {'id': 101, 'age': 21, 'name': 'Amit'} 
city : New Delhi  
Sub-Dictionary 'Record' keys & values...  
id : 101 
age : 21 
name : Amit

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Generate dictionary of numbers and their ... >>
<< Python program to create a dictionary with integer...