{"id":5063,"date":"2023-10-26T15:29:41","date_gmt":"2023-10-26T09:59:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=5063"},"modified":"2023-11-10T12:01:07","modified_gmt":"2023-11-10T06:31:07","slug":"convert-dictionary-values-to-a-list-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/convert-dictionary-values-to-a-list-in-python\/","title":{"rendered":"Convert Dictionary Values to a List in Python"},"content":{"rendered":"
Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.<\/p>\n
The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column \u2018:\u2019 separates the value of each key.<\/p>\n
A dictionary cannot be sorted only to get a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you need an ordered data type, which is a list\u2014probably a list of tuples.<\/p>\n
Given a dictionary, the task is to convert values of this dictionary to a list.<\/p>\n
Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to convert dictionary values to a list some of them are:<\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can also use this list comprehension to iterate over the values of dictionary and convert to list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Assume we want to convert only a selected\u00a0values in a dictionary to a list.<\/p>\n As an example, consider converting values greater than 100.<\/p>\n We can accomplish this by iterating through the dictionary values and checking the condition.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair. The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column \u2018:\u2019 separates the value …<\/p>\ndictionary = <\/span>{<\/span>'this'<\/span>: <\/span>200<\/span>, <\/span>'is'<\/span>: <\/span>100<\/span>, <\/span>'BTechGeeks'<\/span>: <\/span>300<\/span>}<\/span><\/pre>\n
[200, 100, 300]<\/pre>\n
dictionary = <\/span>{<\/span>'this'<\/span>: <\/span>200<\/span>, <\/span>'is'<\/span>: <\/span>100<\/span>, <\/span>'BTechGeeks'<\/span>: <\/span>300 ,'python' :400<\/span>}<\/span><\/pre>\n
[200, 100, 300,400]<\/pre>\n
Convert Dictionary Values to a List in Python<\/h2>\n
\n
Method #1:Using list() function<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# converting values to list\r\nvalueslist = list(dictionary.values())\r\n# print the values list\r\nfor value in valueslist:\r\n print(value)\r\n<\/pre>\n
[200, 100, 300]<\/pre>\n
Method #2:Using List Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# converting values to list using list comprehension\r\nvalueslist = ([value for value in dictionary.values()])\r\n# print the values list\r\nprint(valueslist)\r\n<\/pre>\n
[200, 100, 300]<\/pre>\n
Method #3:Convert specific values of dictionary to list<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# Traverse the dictionary values and check condition\r\nvalueslist = [value for value in dictionary.values() if value > 100]\r\n# print the values list\r\nprint(valueslist)\r\n<\/pre>\n
[200, 300]<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n