Iterate through Items of a Dictionary in Python (FOR Loop)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Iterate through Items of a Dictionary in Python (FOR Loop)

Dictionaries are a very useful and widely used data structure in Python. As a developer, you will very often be in situations where you will need to iterate through a dictionary, while you perform some actions on its “key-value” pairs.

○ Iterate through Items() of a Dictionary

When you are working with dictionaries, it is likely that you will want to work with both, the keys and the values. One of the most useful ways to iterate through a dictionary in Python, is by using items(), which is a method that returns a new view of the dictionary’s items – and will contain all of the “key-value” pairs as tuples (key, value), inside of a list. On each iteration, the current tuple will be unpacked in the form of “key, value” so you can either access the key, or the value – individually.

my_dict = {
'age': 27,
'job': 'Youtuber',
'name': 'Digital Academy'
}

for k, v in my_dict.items():
print(k, '-', v)

# OUTPUT:
# age - 27
# job - Youtuber
# name - Digital Academy
# my_dict.items() = [('age', 27),('job','Youtuber'),('name', 'Digital Academy')]



○ Iterate through Keys() of a Dictionary

If you just need to work with the keys of a dictionary, then you can use keys() function, which is a method that returns a new view object, containing the dictionary’s keys. On each iteration, the current item will only be the key – not its associated value!

my_dict = {
'age': 27,
'job': 'Youtuber',
'name': 'Digital Academy'
}

for k in my_dict:
print(k)

for k in my_dict.keys():
print(k)

# OUTPUT:
# 'age'
# 'job'
# 'name'



○ Iterate through Values() of a Dictionary

If you use a dictionary in a for loop, it traverses the keys of the dictionary – by default. But it is also common to only use the values, to iterate through a dictionary in Python. One way to do that is to use values(), which returns a view with the values of the dictionary. On each iteration, the current item will only be the value – not its associated key!

my_dict = {
'age': 27,
'job': 'Youtuber',
'name': 'Digital Academy'
}

for k in my_dict:
print(my_dict[k])

for v in my_dict.values():
print(v)

# OUTPUT:
# 27
# 'Youtuber'
# 'Digital Academy'



○ Dictionary Length

Eventually, you may want to find how many “key: value” pairs a dictionary has. In that case, you do not have to iterate through a dictionary and count each item, but use the function len() – which returns the number of items in a dictionary.

my_dict = {
'age': 27,
'job': 'Youtuber',
'name': 'Digital Academy'
}

print(len(my_dict)) # OUTPUT: 3



Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap