For value in dict python

In Python, to iterate the dictionary (dict) with a for loop, use keys(), values(), items() methods. You can also get a list of all keys and values in the dictionary with those methods and list().

  • Iterate keys in dictionary (dict): keys()
  • Iterate values in dictionary (dict): values()
  • Iterate key-value pairs in dictionary (dict): items()

Use the following dictionary as an example.

d = {'key1': 1, 'key2': 2, 'key3': 3}

You can iterate keys by using the dictionary object directly in a for loop.

for k in d:
    print(k)
# key1
# key2
# key3

Iterate keys in dictionary (dict): keys()

As mentioned above, you can iterate keys by using the dictionary object directly, but you can also use keys(). The result is the same, but keys() may clarify the intent to the reader of the code.

for k in d.keys():
    print(k)
# key1
# key2
# key3

The keys() method returns dict_keys. It can be converted to a list with list().

keys = d.keys()
print(keys)
print(type(keys))
# dict_keys(['key1', 'key2', 'key3'])
# <class 'dict_keys'>

k_list = list(d.keys())
print(k_list)
print(type(k_list))
# ['key1', 'key2', 'key3']
# <class 'list'>

You can use dict_keys to perform set operations. See the following article for details.

  • Set operations on multiple dictionary keys in Python

Iterate values in dictionary (dict): values()

You can iterate values in the dictionary with the values() method.

for v in d.values():
    print(v)
# 1
# 2
# 3

The values() method returns dict_values. It can be converted to a list with list().

values = d.values()
print(values)
print(type(values))
# dict_values([1, 2, 3])
# <class 'dict_values'>

v_list = list(d.values())
print(v_list)
print(type(v_list))
# [1, 2, 3]
# <class 'list'>

Iterate key-value pairs in dictionary (dict): items()

You can iterate key-value pairs in the dictionary with the items() method.

for k, v in d.items():
    print(k, v)
# key1 1
# key2 2
# key3 3

It can also be received as a tuple of (key, value).

for t in d.items():
    print(t)
    print(type(t))
    print(t[0])
    print(t[1])
    print('---')
# ('key1', 1)
# <class 'tuple'>
# key1
# 1
# ---
# ('key2', 2)
# <class 'tuple'>
# key2
# 2
# ---
# ('key3', 3)
# <class 'tuple'>
# key3
# 3
# ---

The items() method returns dict_items. It can be converted to a list with list().

items = d.items()
print(items)
print(type(items))
# dict_items([('key1', 1), ('key2', 2), ('key3', 3)])
# <class 'dict_items'>

i_list = list(d.items())
print(i_list)
print(type(i_list))
# [('key1', 1), ('key2', 2), ('key3', 3)]
# <class 'list'>

print(i_list[0])
print(type(i_list[0]))
# ('key1', 1)
# <class 'tuple'>

You can also use dict_items to perform set operations. See the following article for details.

  • Set operations on multiple dictionary keys in Python

How do you check if a value is in a dictionary in Python?

Check if a value exists in a dictionary: in operator, values() To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use not in to check if a value does not exist in a dictionary.

What does dict values () return in Python?

The methods dict. keys() and dict. values() return lists of the keys or values explicitly. There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary.

Can Python dictionary have function as value?

Given a dictionary, assign its keys as function calls. Case 1 : Without Params. The way that is employed to achieve this task is that, function name is kept as dictionary values, and while calling with keys, brackets '()' are added.

Can you loop through a dictionary Python?

You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.