Get all values from a dictionary Python

In this Python tutorial, we will discuss the Get all values from a dictionary Python. Here we will also cover the below examples:

  • Get all values from nested dictionary Python
  • Get all values from list of dictionary Python
  • Get all key values from dictionary Python
  • Python get all key values from nested dictionary
  • Get all max values from dictionary Python
  • Python get all unique values from dictionary
  • Get all key values from list of dictionary Python
  • Get all key values from dictionary Python
  • Get all values from dictionary Python with index

Get all values from a dictionary Python

  • In Python to get all values from a dictionary, we can use the values() method. The values() method is a built-in function in Python and returns a view object that represents a list of dictionaries that contains all the values.
  • There are many method to execute this task
    • By using values method
    • By using list comprehension method
    • By using for loop method

Source Code:

my_dictionary = {'k' : 47, 'l' : 19, 'm' : 92}

new_output = list(my_dictionary.values())
print(" all values in dictionary are:",new_output)

In the above code first, we will initialize a dictionary and assign key-value pair elements. Now use the list() method and store an original dictionary as a parameter.

Here is the execution of the following given code

Get all values from a dictionary Python
Get all values from a dictionary Python

By using list comprehension method

In Python list comprehension is defined by square brackets[] and it is used to create a new list from iterable items like a dictionary. In this example, we will extract all the values from a dictionary and contains them into the list.

Example:

new_dict = {"Australia":56,"SouthAfrica":91,"NewZealand":189}

[print(new_value) for new_value in new_dict.values()]

Here is the Screenshot of the following given code

Get all values from a dictionary Python by list method
Get all values from a dictionary Python by list method

By using for loop method

In this example, we can use the values() method in a for loop syntax and it will return iterable values.

Source Code:

stu_dict ={"j":18,"q":56,"o":119}
for new_val in stu_dict.values():
    print("Extract values from dictionary",new_val)

Here is the Output of the following given code

Get all values from a dictionary Python by loop method
Get all values from a dictionary Python by the loop method

Read: Python dictionary of tuples

Get all values from nested dictionary Python

  • Let us see how to get all values from the nested dictionary in Python.
  • To extract all values from a nested dictionary we can easily use the values() method. This method is used to store all the values from a dictionary. In Python, the values() method will check the condition if the dictionary does not contain any value then it will return an empty dictionary.

Source Code:

def new_nest_dict(m):
  for val in m.values():
    if isinstance(val, dict):
      yield from new_nest_dict(val)
    else:
      yield val

my_dict={'g':19,'k':32,'z':{'i':89,'q':10,'pa':{'ou':10},'w':6,'h':{'rt':17,'lr':16}}}
b= list(new_nest_dict(my_dict))
print(b)

In the above code first, we will define a function and pass the ‘m’ keyword as an argument. Now we will check the condition of the ‘isinstance’ method this method returns True if the given value is contained in an argument. In this example, we will extract all the values from a nested dictionary and contains them into the list.

Here is the execution of the following given Code

Get all values from nested dictionary Python
Get all values from nested dictionary Python

Read: Python creates a dictionary from two lists

Get all values from list of dictionary Python

In Python to get all values from a list of dictionaries, we can apply the list comprehension method. In this example first, we have created a list and store a nested dictionary pair. After that, we use the list comprehension method and pass the ‘Country_name’ key as an argument in the list.

Example:

new_lis_dict = [{'Country_name': 24, 'stu_name': 190},
           {'Country_name': 89, 'stu_name': 678},
           {'Country_name': 129, 'stu_name': 292}]
 
new_val = [m['Country_name'] for m in new_lis_dict if 'Country_name' in m]
new_val2 = [m['stu_name'] for m in new_lis_dict if 'stu_name' in m]
print(new_val)
print(new_val2)

Here is the Output of the following given code

Get all values from list of dictionary Python
Get all values from a list of dictionary Python

Read: Python dictionary pop

Get all key values from dictionary Python

In Python to get all key-values pair from the dictionary, we can easily use the method dict. items(). This method helps the user to extract all keys and values from the dictionary and iterate the object with a for loop method.

Source Code:

stu_dict ={"Germany":178,"Egypt":145,"Ukraine":129}
 
for key, value in stu_dict.items():
    print("Extract keys and values from dictionary:",key,value)

Here is the Screenshot of the following given code

Get all key values from dictionary Python
Get all key values from dictionary Python

Read: Python loop through a list

Python get all key values from nested dictionary

  • Let us see how to get all key values pair from a nested dictionary.
  • To do this task we can apply the dict.items() method. This method extracts all the elements from a nested dictionary.

Source Code:

my_dict = {
    'Country_name':{'Albania':278,'Bermuda':197,'Colombia':156},
    'student_name':{'Florida':146,'Elanie':467,'Malia': 125,'Adelina':886,'Oliva':997}
}
 
for key, value in my_dict.items():
    print("Extract pairs from nested dictionary:",key,value)

In the above, we have created a list and contains key-value pair elements in a nested dictionary. Now use a for loop method to get the key-value pair from a dictionary.

Here is the implementation of the following given code

Python get all key values from nested dictionary
Python get all key values from a nested dictionary

Read: Python dictionary contains

Get all max values from dictionary Python

  • Let us see how to find a maximum value from the dictionary.
  • By using max() and dict.values() we can get the maximum value from the Python dictionary.
  • In Python the max() function return the largest value from a given dictionary. The values() method always returns a view object that represents a list of dictionaries which contains all the values.

Example:

my_new_dict = {"m": 18, "q": 92, "z": 199}

new_value = my_new_dict.values()
b = max(new_value)
print("Maximum value from dictionary:",b)

In the above code first, we initialize a dictionary and assign them key-value pair elements. Now create a variable and pass dict. values() as an argument. After that use max() function to get a maximum value from the dictionary.

Here is the Output of the following given code

Get all max values from dictionary Python
Get all max values from dictionary Python

Read: Python dictionary comprehension

Python get all unique values from dictionary

  • Let us see how to get unique values from a dictionary.
  • To perform this task we can use set() method and pass list comprehension method as an argument. In Python the set() method is a built-in function in Python and it is used to convert any iterable element where in this iterable is ‘new_list’ variable.

Source Code:

new_list = [{"q":"t02"}, {"k": "t01"}, {"z": "t02"}, {"z": "t09"}, {"m":"t03"}, {"u":"t01"},{"v":"t04"}]

new_val = set( new_v for new_dic in new_list for new_v in new_dic.values())
print("Unique Values from dictionary: ",new_val)

In this example, I have to print a list of unique values and remove duplicate values from a dictionary.

Here is the execution of the following given code

Python get all unique values from dictionary
Python get all unique values from a dictionary

Read: Python dictionary find a key by value

Get all values from dictionary Python with index

  • Here we can check how to get all values from a dictionary with an index in Python.
  • By using the dictionary list comprehension and enumerator method we can easily perform this task.
  • To create a list in Python we can easily use the list comprehension method it is easy way to get the values from dictionary. In this example we can apply enumerator() method with for loop to track the position of our element.
  • These functions are used to get all values from dictionary Python.

Example:

Let’s take an example and check how to get all values from a dictionary with an index

new_list = [4,5,6,7,8,9]

new_out = {val : m + 1 for m, val in enumerate(new_list)}
  
print("Extract values with index: ",new_out)

Here is the Screenshot of the following given code

Get all values from dictionary Python with index
Get all values from dictionary Python with index

You may also like reading the following articles.

In this Python tutorial, we have discussed the Get all values from a dictionary Python. Here we have also covered the following topics:

  • Get all values from nested dictionary Python
  • Get all values from list of dictionary Python
  • Get all key values from dictionary Python
  • Python get all key values from nested dictionary
  • Get all max values from dictionary Python
  • Python get all unique values from dictionary
  • Get all key values from list of dictionary Python
  • Get all key values from dictionary Python
  • Get all values from dictionary Python with index