Python Iterate Dict and Python Loop Through Dictionary Example

Python Iterate Dictionary Key Value

Each key is separated by a colon (:) from its value, items are separated by commas, and the whole thing is enclosed in a curved brace. An empty dictionary with no items is written with only two curly braces, such as: {}.

Keys are unique within a dictionary while values ​​cannot be. The values ​​of a dictionary can be of any type, but the key must be of an immutable data type such as strings, numbers, or tuples.

Access value in a dictionary

To access dictionary elements, you can use square brackets familiar with the key to get its value.

learn more about dictionary in python:

Dictionary using for loop in python

user1 = {'Name':'Pramod', 'Age':22, 'Position': 'Engineer'}
for i in user1:       # To check key in dictionary
    print(i)
#To check all value presents in dictionary
print('After using value function',)
for i in user1.values():
    print(i)

Output:

Name
Age
Position
After using value function
Pramod
22
Engineer

Value Method in Python | Iterate Dictionary Python

Python dictionary value() method The python values​​() method is used to gather all values ​​from a dictionary. It takes no parameters and gives a dictionary of values. If the dictionary has no value then it returns an empty dictionary. The syntax and examples of this method are given below. signature Values()  Parameter  No parameters The return    It gives a dictionary of values. See some examples of the values() method to understand values? working capacity.  

Python dictionary value() method example -1

This is a simple example that returns all values ​​from the dictionary. An example is given below.

# Python dictionary value () method 
# Creating a Dictionary
inventory = {'fan': 200, 'bulb': 150, 'LED': 1000}
# Calling function
share = inventory.values ()
# Displaying Results 
print ("stock available", share)
Output:
stock available dict_values([200, 150, 1000])

Python dictionary value () method example 2

If the dictionary is already empty, this method also returns an empty dictionary except for any errors or exceptions. See an example below.

# Python dictionary  value () method 
# Creating a Dictionary
inventory = {}
# Calling function
share = inventory.values()
# Displaying Results
print ("stock available", inventory)
Output:
stock available{}

Python iterate dict value() method example -3

This example uses various methods such as length, keys, etc. to get information about the dictionary.

# Python dictionary value () method
# Creating a Dictionary
inventory = {'fan': 200, 'bulb': 150, 'LED': 1000}
# to work
length = len (inventory)
print ("Total number of values:", length)
keys = inventory.keys()
print ("All keys:", keys)
item = inventory.items ()
print ("item:", inventory)
p = inventory.popitem()
print ("Deleted Items:", p)
share = inventory.values()
print ("stock available", share)
OutPut:
Total number of values: 3
All keys: dict_keys(['fan', 'bulb', 'LED'])
item: {'fan': 200, 'bulb': 150, 'LED': 1000}
Deleted Items: ('LED', 1000)
stock available dict_values([200, 150])
Python iterate dict value() method example

Example of python iterate dict -4

user_info = {'Name':'pramod', 'Age':22,'RollNo':'17691a05b9','Class':'B-Tech'}
 user_info_value = user_info.values()
 user_info_value = user_info.values()
 print(user_info_value)
Output:
dict_values(['pramod', 22, '17691a05b9', 'B-Tech'])

Python loop through dictionary -5

user_info = {'Name':'pramod', 'Age':22,'RollNo':'17691a05b9','Class':'B-Tech'}

print('Print value of dict')
for i in user_info.keys():
    print(user_info[i])

print('Print keys of dict')
user_info_value2 = user_info.keys()
print(user_info_value2)       
Output:
Print value of dict
 pramod
 22
 17691a05b9
 B-Tech

 Print keys of dict
 dict_keys(['Name', 'Age', 'RollNo', 'Class'])
Python loop through dictionary

Recommended Posts:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment