Title: How to Iterate Through a Dictionary in Python – Real Python
Open Graph Title: How to Iterate Through a Dictionary in Python – Real Python
Description: In this tutorial, you'll take a deep dive into how to iterate through a dictionary in Python. Dictionaries are a fundamental data type in Python, and you can solve various programming problems by iterating through them.
Open Graph Description: In this tutorial, you'll take a deep dive into how to iterate through a dictionary in Python. Dictionaries are a fundamental data type in Python, and you can solve various programming problems by iterating through them.
Mail addresses
?subject=Python article for you&body=How to Iterate Through a Dictionary in Python on Real Python
https://realpython.com/iterate-through-dictionary-python/
Opengraph URL: https://realpython.com/iterate-through-dictionary-python/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "How to Iterate Through a Dictionary in Python",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/How-to-Iterate-Through-A-Dictionary-in-Python_Watermarked.06d6547f531b.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/iterate-through-dictionary-python/",
"lastReviewed": "2024-11-23",
"author": {
"@type": "Person",
"name": "Leodanis Pozo Ramos",
"image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
"url": "https://realpython.com/team/lpozoramos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Aldren Santos",
"image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
"url": "https://realpython.com/team/asantos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Brenda Weleschuk",
"image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
"url": "https://realpython.com/team/bweleschuk/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Geir Arne Hjelle",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
"url": "https://realpython.com/team/gahjelle/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Jim Anderson",
"image": "https://realpython.com/cdn-cgi/image/width=700,height=700,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jima.0b8f990b951a.jpg",
"url": "https://realpython.com/team/janderson/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Joanna Jablonski",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/jjablonksi-avatar.e37c4f83308e.jpg",
"url": "https://realpython.com/team/jjablonski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Kate Finegan",
"image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg",
"url": "https://realpython.com/team/kfinegan/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Martin Breuss",
"image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
"url": "https://realpython.com/team/mbreuss/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2024-11-23T14:00:00+00:00",
"dateModified": "2024-11-23T14:09:25.886830+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Leodanis Pozo Ramos",
"image": "https://realpython.com/cdn-cgi/image/width=862,height=862,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Perfil_final1.9f896bc212f6.jpg",
"url": "https://realpython.com/team/lpozoramos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "In this tutorial, you'll take a deep dive into how to iterate through a dictionary in Python. Dictionaries are a fundamental data type in Python, and you can solve various programming problems by iterating through them.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What\u2019s the difference between iterating with .keys() and .values()?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Using .keys() returns a view of the dictionary’s keys, allowing you to iterate through them. Conversely, .values() returns a view of the dictionary’s values. If you only need to work with keys or values, you can choose the appropriate method to make your code more explicit and readable.
"
}
},
{
"@type": "Question",
"name": "How do you iterate over a dictionary\u2019s keys and values in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "You can iterate over a dictionary’s keys and values using the .items() method. This method returns a view object containing key-value pairs as tuples, which you can unpack directly in a for loop:
\n\n \n Python\n \n \n \n \n \n \n for key, value in dict_object.items():\n print(key, value)\n
\n \n \n \n \n \nIn this example, you use key, value to unpack the tuples that you create during the iteration with .items().
"
}
},
{
"@type": "Question",
"name": "Can I iterate over a dictionary while modifying its content?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, you can! If you only need to update or modify the values of an existing dictionary, then you can safely do it in a loop because dictionaries are mutable, and you can change their values in place.
\nOn the other hand, if you need to remove key-value pairs from a dictionary while you’re looping through it, then you’d need to use a copy for the iteration and remove the values from the original dictionary. To create a shallow copy of a dictionary, you can use the .copy() method.
\nYou can also use a while loop and the .popitem() method to remove consecutive items from a dictionary without needing to make a copy beforehand.
\nSometimes, a better option is to use a dictionary comprehension to create a new dictionary with transformed values.
"
}
},
{
"@type": "Question",
"name": "How can I iterate through a dictionary in a specific order?",
"acceptedAnswer": {
"@type": "Answer",
"text": "If you want to iterate through a dictionary in sorted order, for example, then you can use the built-in sorted() function. If you feed this function with the result of .items(), then you’ll get a list of key-value tuples that you can traverse in a loop as usual.
\nAdditionally, you can use the built-in reversed() function to iterate through a Python dictionary in reverse order.
"
}
},
{
"@type": "Question",
"name": "How can I iterate through multiple dictionaries in one go?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The collections and itertools standard-library modules provide the ChainMap class and the chain() function, respectively. Both tools allow you to combine multiple dictionaries and iterate through them in a chain. You’ll find a subtle behavior difference between these tools, though.
\nWhen the input dictionaries have duplicate keys, ChainMap only gives you access to the first instance of a key, while chain() gives you access to all the instances of a repeated key.
\nFinally, you can also use the dictionary unpacking operator (**) to merge multiple dictionaries together and iterate over the resulting dictionary. In this case, if the input dictionaries have duplicate keys, the last instance of every repeated key will prevail.
"
}
}
]
}
}
| author | Real Python |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/How-to-Iterate-Through-A-Dictionary-in-Python_Watermarked.06d6547f531b.jpg |
| og:image | https://files.realpython.com/media/How-to-Iterate-Through-A-Dictionary-in-Python_Watermarked.06d6547f531b.jpg |
| twitter:creator | @realpython |
| og:type | article |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large