Python - Access Dictionary items Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report A dictionary in Python is a useful way to store data in pairs, where each key is connected to a value. To access an item in the dictionary, refer to its key name inside square brackets.Example: Python a = {"Geeks": 3, "for": 2, "geeks": 1} #Access the value assosiated with "geeks" x = a["geeks"] print(x) Output1 Let's explore various ways to access keys, values or items in the Dictionary.Table of ContentUsing get() MethodAccessing All Keys, Values, and ItemsUsing key() MethodUsing values() MethodUsing 'in' OperatorUsing dict.items()Using get() MethodWe can access dictionary items by using get() method. This method returns None if the key is not found instead of raising an error. Additionally, we can specify a default value that will be returned if the key is not found: Python a = {"Geeks": 3, "for": 2, "geeks": 1} #Accessing value associated with "for" value = a.get("for") print(value) Output2 Accessing Keys, Values, and ItemsBy using methods like keys(), values(), and items() methods we can easily access required items from the dictionary. Get all Keys Using key() Method In Python dictionaries, keys() method returns a view of the keys. By iterating through this view using a for loop, you can access keys in the dictionary efficiently. This approach simplifies key-specific operations without the need for additional methods. Python a = {"geeks": 3, "for": 2, "Geeks": 1} #Looping through the keys of the dictionary keys = a.keys() print(keys) Outputdict_keys(['geeks', 'for', 'Geeks']) Get all values Using values() Method In Python, we can access the values in a dictionary using the values() method. This method returns a view of all values in the dictionary, allowing you to iterate through them using a for loop or convert them to a list. Python a = {"geeks": 3, "for": 2, "Geeks": 1} # Accessing values using values() method values = a.values() print(values ) Outputdict_values([3, 2, 1]) Using 'in' OperatorThe in is most used method that can get all the keys along with its value, the "in" operator is widely used for this very purpose and highly recommended as it offers a concise method to achieve this task. Python a = {'geeks': 3, 'for': 2, 'Geeks': 1} # Looping through each key in the dictionary 'a' for key in a: print(key, a[key]) Outputgeeks 3 for 2 Geeks 1 Get key-value Using dict.items()The items() method allows you to iterate over both keys and values simultaneously. It returns a view of key-value pairs in the dictionary as tuples. Python a = {'geeks': 3, 'for': 2, 'Geeks': 1} # Iterating through key-value pairs for key, value in a.items(): print(f"{key}: {value}") Outputgeeks: 3 for: 2 Geeks: 1 Also Read:Python Dictionary MethodsPython Dictionary get() MethodCheck whether given Key already exists in DictionarySort Python Dictionaries by Key or ValueAccessing Items in Lists Within Dictionary Comment More infoAdvertise with us Next Article Python - Access Dictionary items manjeet_04 Follow Improve Article Tags : Python python-dict Python dictionary-programs Practice Tags : pythonpython-dict Similar Reads Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to 5 min read How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa 3 min read Python - Add Dictionary Items In Python, dictionaries are a built-in data structure that stores key-value pairs. Adding items to a dictionary is a common operation when you're working with dynamic data or building complex data structures. This article covers various methods for adding items to a dictionary in Python.Adding Items 3 min read Python Add Dictionary Key In Python, dictionaries are unordered collections of key-value pairs. Each item in a dictionary is accessed by its unique key, which allows for efficient storage and retrieval of data. While dictionaries are commonly used with existing keys, adding a new key is an essential operation when working wi 4 min read Python Access Dictionary In Python, dictionaries are powerful and flexible data structures used to store collections of data in key-value pairs. To get or "access" a value stored in a dictionary, we need to know the corresponding key. In this article, we will explore all the ways to access dictionary values, keys, and both 4 min read Python Change Dictionary Item A common task when working with dictionaries is updating or changing the values associated with specific keys. This article will explore various ways to change dictionary items in Python.1. Changing a Dictionary Value Using KeyIn Python, dictionaries allow us to modify the value of an existing key. 3 min read Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop 2 min read Get length of dictionary in Python Python provides multiple methods to get the length, and we can apply these methods to both simple and nested dictionaries. Letâs explore the various methods.Using Len() FunctionTo calculate the length of a dictionary, we can use Python built-in len() method. It method returns the number of keys in d 3 min read Python - Value length dictionary Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor 4 min read Python - Dictionary values String Length Summation Sometimes, while working with Python dictionaries we can have problem in which we need to perform the summation of all the string lengths which as present as dictionary values. This can have application in many domains such as web development and day-day programming. Lets discuss certain ways in whi 4 min read Like