Sayantan's Blog On Python Programming

PYTHON STRING KEY VALUE IN DICTIONARY

PYTHON STRING KEY VALUE IN DICTIONARY

INTRODUCTION

Key and value pair both can be string in python dictionaries.

Accessing Values

Using Keys

# Dictionary string key value pair example 1

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

#Accessing values against keys

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

Value of key A in dict_class_wise_prog -> Python
Value of key B in dict_class_wise_prog -> Java
Value of key C in dict_class_wise_prog -> C++
Value of key D in dict_class_wise_prog -> PHP
Value of key E in dict_class_wise_prog -> XML
Value of key F in dict_class_wise_prog -> VB

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

Using values() function

# Dictionary string key value pair example 2

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

#Accessing all values against keys

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

List of values of dict_class_wise_prog -> dict_values(['Python', 'Java', 'C++', 'PHP', 'XML', 'VB'])

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

Using list() function

# Dictionary string key value pair example 3

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

#Accessing all values against keys

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

1st value of dict_class_wise_prog -> Python
2nd value of dict_class_wise_prog -> Java
3rd value of dict_class_wise_prog -> C++
4th value of dict_class_wise_prog -> PHP
5th value of dict_class_wise_prog -> XML
6th value of dict_class_wise_prog -> VB

Accessing Keys

Using keys() function

# Dictionary string key value pair example 4

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

#Accessing all keys

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

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

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

Using list() function

# Dictionary string key value pair example 5

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

#Accessing all keys

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

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

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

Counting No of Keys

# Dictionary string key value pair example 6

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}

Number of key value pair in dict_class_wise_prog -> 6

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

PYTHON STRING KEY VALUE IN DICTIONARY : Output
PYTHON STRING KEY VALUE IN DICTIONARY : Output

RELATED TOPICS:

1 thought on “PYTHON STRING KEY VALUE IN DICTIONARY”

  1. Pingback: PYTHON MIXED KEY VALUE IN DICTIONARY - Sayantan's Blog On Python Programming

Leave a Comment

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