{"id":5992,"date":"2023-10-29T10:40:37","date_gmt":"2023-10-29T05:10:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=5992"},"modified":"2023-11-10T12:05:48","modified_gmt":"2023-11-10T06:35:48","slug":"python-how-to-create-a-list-of-all-the-values-in-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-create-a-list-of-all-the-values-in-a-dictionary\/","title":{"rendered":"Python : How to Create a List of all the Values in a Dictionary ?"},"content":{"rendered":"
List:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Dictionary:<\/strong><\/p>\n 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.<\/p>\n Given a dictionary, the task is to convert the values of dictionary to list.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/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\u2019s values if we transfer this iterable sequence(dict values) to it.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ndictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200}<\/pre>\n
[400, 900, 200]<\/pre>\n
Create a List of all the Values in a Dictionary<\/h2>\n
\n
1)Using list() and values() function<\/h3>\n
# given_dictionary \r\ndictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200} \r\n# creating a list from dictionary values \r\nlist_of_values = list(dictionary.values()) \r\n# print the newly created list \r\nprint(list_of_values)<\/pre>\n
[400,900,200]<\/pre>\n
2)Making a list of duplicate dictionary values<\/h3>\n
\n
# given_dictionary\r\ndictionary = {'Hello': 200, 'BtechGeeks': 900,\r\n 'Platform': 200, 'online': 400, 'python': 400}\r\n# taking two empty list\r\nuniq_values = []\r\ndup_values = []\r\n# Traverse the values of the dictionary\r\nfor value in dictionary.values():\r\n if value not in uniq_values:\r\n uniq_values.append(value)\r\n else:\r\n dup_values.append(value)\r\n\r\n# print the duplicate values\r\nprint(dup_values)<\/pre>\n
[200, 400]<\/pre>\n
\n