Sayantan's Blog On Python Programming

PYTHON NUMBER KEY VALUE IN DICTIONARY

PYTHON NUMBER KEY VALUE IN DICTIONARY

INTRODUCTION

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

Accessing Values

Using Keys

# Dictionary numeric key value pair example 1

dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

#Accessing values against keys

print("\nValue of key 2000 in dict_year_count ->", dict_year_count[2020])
print("Value of key 2021 in dict_year_count ->", dict_year_count[2021])
print("Value of key 2022 in dict_year_count ->", dict_year_count[2022])
print("Value of key 2023 in dict_year_count ->", dict_year_count[2023])
print("Value of key 2024 in dict_year_count ->", dict_year_count[2024])
print("Value of key 2025 in dict_year_count ->", dict_year_count[2025])

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}

Value of key 2000 in dict_year_count -> 100
Value of key 2021 in dict_year_count -> 200
Value of key 2022 in dict_year_count -> 300
Value of key 2023 in dict_year_count -> 700
Value of key 2024 in dict_year_count -> 500
Value of key 2025 in dict_year_count -> 600

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

Using values() function

# Dictionary numeric key value pair example 2

dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

# Accessing values against keys
print("\nList of values of dict_year_count ->", dict_year_count.values())

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}

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

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

Using list() function

# Dictionary numeric key value pair example 3

dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

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

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}

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

In the above example, we have used the list() function to convert the dictionary into a list object and then accessed the individual values of the dictionary dict_year_count.

# Dictionary numeric key value pair example 4

dict_year_count = {2020:10.5, 2021:2.8, 2022:3.7, 2023:4.9, 2024:5.8, 2025:6.1, 2023:7.3}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

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

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 10.5, 2021: 2.8, 2022: 3.7, 2023: 7.3, 2024: 5.8, 2025: 6.1}

1st value of dict_year_count -> 10.5
2nd value of dict_year_count -> 2.8
3rd value of dict_year_count -> 3.7
4th value of dict_year_count -> 7.3
5th value of dict_year_count -> 5.8
6th value of dict_year_count -> 6.1

Accessing Keys

Using keys() function

# Dictionary numeric key value pair example 5

dict_year_count = {2020:10.5, 2021:2.8, 2022:3.7, 2023:4.9, 2024:5.8, 2025:6.1, 2023:7.3}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

# Accessing values against keys

print("\nList of keys of dict_year_count ->", dict_year_count.keys())

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 10.5, 2021: 2.8, 2022: 3.7, 2023: 7.3, 2024: 5.8, 2025: 6.1}

List of keys of dict_year_count -> dict_keys([2020, 2021, 2022, 2023, 2024, 2025])

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

Using list() function

# Dictionary numeric key value pair example 6

dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

#Accessing keys

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

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}

1st key of dict_year_count -> 2020
2nd key of dict_year_count -> 2021
3rd key of dict_year_count -> 2022
4th key of dict_year_count -> 2023
5th key of dict_year_count -> 2024
6th key of dict_year_count -> 2025

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

Counting No of Keys

# Dictionary numeric key value pair example 7

dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)

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

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}

Number of key value pair in dict_year_count -> 6

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

PYTHON NUMBER KEY VALUE IN DICTIONARY : Output
PYTHON NUMBER KEY VALUE IN DICTIONARY : Output

RELATED TOPICS:

1 thought on “PYTHON NUMBER KEY VALUE IN DICTIONARY”

  1. Pingback: UPDATING PYTHON DICTIONARIES - Sayantan's Blog On Python Programming

Leave a Comment

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