How to Create a List of all the Values in a Dictionary

Python : How to Create a List of all the Values in a Dictionary ?

List:

A collection is a set of values that are arranged in a specific order. There could be different kinds of values. A list is a container that can be changed. Existing ones can be added to, deleted from, or changed as a result.

The Python list is a mathematical representation of the concept of a finite sequence. List items or list elements are the names given to the values in a list. A list may contain multiple instances of the same value. Each event is treated as a separate element.

Dictionary:

Dictionaries are Python’s implementation of an associative list, which can be a collection. A dictionary can be defined as a collection of key-value pairs that are kept together. Each key-value pair represents a key and its value.

Given a dictionary, the task is to convert the values of dictionary to list.

Examples:

Input:

dictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200}

Output:

[400, 900, 200]

Create a List of all the Values in a Dictionary

1)Using list() and values() function

A dictionary class in Python has a function values() that returns an iterable sequence of all dictionary values. The list() function will return a list containing all of the dictionary’s values if we transfer this iterable sequence(dict values) to it.

Below is the implementation:

# given_dictionary 
dictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200} 
# creating a list from dictionary values 
list_of_values = list(dictionary.values()) 
# print the newly created list 
print(list_of_values)

Output:

[400,900,200]

2)Making a list of duplicate dictionary values

Approach:

  1. Take a two empty lists named dup_values and uniq_values
  2. Traverse the values of the dictionary
  3. If the value is not in uniq_values then append the element to it.
  4. Else append that element to dup_values.
  5. Print the dup_values list

Below is the implementation:

# given_dictionary
dictionary = {'Hello': 200, 'BtechGeeks': 900,
              'Platform': 200, 'online': 400, 'python': 400}
# taking two empty list
uniq_values = []
dup_values = []
# Traverse the values of the dictionary
for value in dictionary.values():
    if value not in uniq_values:
        uniq_values.append(value)
    else:
        dup_values.append(value)

# print the duplicate values
print(dup_values)

Output:

[200, 400]

Related Programs: