Sayantan's Blog On Python Programming

PYTHON MIXED KEY VALUE IN DICTIONARY

PYTHON MIXED KEY VALUE IN DICTIONARY

INTRODUCTION

Key and value pair both can be of different data type in python dictionaries. Keys can be of string data type and values can be of number data type or vice-versa.

Accessing Values

Using Keys

# Dictionary mixed key value pair example 1

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

#Accessing values against keys

print("\nValue of key A in dict_class_wise_count ->", dict_class_wise_count['A'])
print("Value of key B in dict_class_wise_count ->", dict_class_wise_count['B'])
print("Value of key C in dict_class_wise_count ->", dict_class_wise_count['C'])
print("Value of key D in dict_class_wise_count ->", dict_class_wise_count['D'])
print("Value of key E in dict_class_wise_count ->", dict_class_wise_count['E'])
print("Value of key F in dict_class_wise_count ->", dict_class_wise_count['F'])

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

Value of key A in dict_class_wise_count -> 100
Value of key B in dict_class_wise_count -> 200
Value of key C in dict_class_wise_count -> 300
Value of key D in dict_class_wise_count -> 400
Value of key E in dict_class_wise_count -> 500
Value of key F in dict_class_wise_count -> 600

In the above example, we have accessed all the values against keys in dictionary dict_class_wise_count.

Using values() function

# Dictionary mixed key value pair example 2

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

#Accessing values against keys

print("\nList of values of dict_class_wise_count ->", dict_class_wise_count.values())

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

List of values of dict_class_wise_count -> dict_values([100, 200, 300, 400, 500, 600])

In the above example, we have used the values() function to accessed values of dictionary dict_class_wise_count.

Using list() function

# Dictionary mixed key value pair example 3

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

#Accessing values against keys

print("\n1st value of dict_class_wise_count ->", list(dict_class_wise_count.values())[0])
print("2nd value of dict_class_wise_count ->", list(dict_class_wise_count.values())[1])
print("3rd value of dict_class_wise_count ->", list(dict_class_wise_count.values())[2])
print("4th value of dict_class_wise_count ->", list(dict_class_wise_count.values())[3])
print("5th value of dict_class_wise_count ->", list(dict_class_wise_count.values())[4])
print("6th value of dict_class_wise_count ->", list(dict_class_wise_count.values())[5])

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

1st value of dict_class_wise_count -> 100
2nd value of dict_class_wise_count -> 200
3rd value of dict_class_wise_count -> 300
4th value of dict_class_wise_count -> 400
5th value of dict_class_wise_count -> 500
6th value of dict_class_wise_count -> 600

In the above example, we have used the list() function to access the individual values in the dictionary.

# Dictionary nested value example 4

dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)

#Accessing values against keys

print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.values())

#Accessing individual values against individual keys

print("\n1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0])

# Accessing list of nested values

print("\n1st element 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][0])
print("2nd element 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][1])
print("3rd element 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][2])

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}

List of values of dict_class_wise_stud_list -> dict_values([[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602]])

1st value of dict_class_wise_stud_list -> [100, 101, 102]

1st element 1st value of dict_class_wise_stud_list -> 100
2nd element 1st value of dict_class_wise_stud_list -> 101
3rd element 1st value of dict_class_wise_stud_list -> 102

In the above example, we have accessed the nested elements of a value against a key.

Accessing Keys

Using keys() function

# Dictionary mixed key value pair example 4

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

#Accessing all keys

print("\nKeys of dict_class_wise_count ->", dict_class_wise_count.keys())

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

Keys of dict_class_wise_count -> dict_keys(['A', 'B', 'C', 'D', 'E', 'F'])

In the above example, we have listed all the keys of dictionary dict_class_wise_count by using keys() function.

Using list() function

# Dictionary mixed key value pair example 5

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

#Accessing all keys

print("\n1st key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[0])
print("2nd key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[1])
print("3rd key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[2])
print("4th key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[3])
print("5th key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[4])
print("6th key of dict_class_wise_count ->", list(dict_class_wise_count.keys())[5])

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

1st key of dict_class_wise_count -> A
2nd key of dict_class_wise_count -> B
3rd key of dict_class_wise_count -> C
4th key of dict_class_wise_count -> D
5th key of dict_class_wise_count -> E
6th key of dict_class_wise_count -> F

In the above example, we have used the list() function to print the individual key of dictionary dict_class_wise_count.

Counting No of Keys

# Dictionary mixed key value pair example 6

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

print("\nNumber of key value pair in dict_class_wise_count ->", len(dict_class_wise_count))

PYTHON MIXED KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}

Number of key value pair in dict_class_wise_count -> 6

In the above example, we have used len() function to print the number of key value pair in the dictionary dict_class_wise_count.

PYTHON MIXED KEY VALUE IN DICTIONARY : Output
PYTHON MIXED KEY VALUE IN DICTIONARY : Output

RELATED TOPICS:

1 thought on “PYTHON MIXED KEY VALUE IN DICTIONARY”

  1. Pingback: PYTHON DICTIONARY SORTING OPERATIONS - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *