{"id":5321,"date":"2021-05-07T11:14:32","date_gmt":"2021-05-07T05:44:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=5321"},"modified":"2021-11-22T18:42:53","modified_gmt":"2021-11-22T13:12:53","slug":"python-how-to-create-a-list-of-all-the-keys-in-the-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-create-a-list-of-all-the-keys-in-the-dictionary\/","title":{"rendered":"Python : How to Create a List of all the Keys in the Dictionary ?"},"content":{"rendered":"
Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0<\/span>arrangement\u00a0<\/span>. A dictionary\u00a0may be a\u00a0<\/span>collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to create a list of all keys of a dictionary some of them are:<\/p>\n The dictionary class in Python has a member function called dict.keys ()<\/p>\n It returns a view object or an iterator that iterates through the list of all keys in the dictionary. This object can be used for iteration or to create a new list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Unpacking with * works with any iterable object, and because dictionaries return their keys when iterated through, it is simple to create a list by using it within a list literal.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can convert to list using list comprehension in a single line.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0arrangement\u00a0. A dictionary\u00a0may be a\u00a0collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair. Examples: Input: dictionary = {‘This’: 100, ‘is’: 200, ‘BTechGeeks’: 300} Output: [‘This’, ‘is’, ‘BTechGeeks’] Create a List of all keys of a …<\/p>\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}<\/pre>\n
['This', 'is', 'BTechGeeks']<\/pre>\n
Create a List of all keys of a Dictionary<\/h2>\n
\n
Method #1:Using keys() +list() function<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# converting the keys of dictionary using keys() +list() function\r\nkeyslist = list(dictionary.keys())\r\n# print the list of keys\r\nprint(keyslist)\r\n<\/pre>\n
['This', 'is', 'BTechGeeks']<\/pre>\n
Method #2 :Using unpacking (*args)<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# converting the keys of dictionary using args(*) function\r\nkeyslist = [*dictionary]\r\n# print the list of keys\r\nprint(keyslist)\r\n<\/pre>\n
['This', 'is', 'BTechGeeks']<\/pre>\n
Method #3 :Using List Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# converting the keys of dictionary using list comprehension\r\nkeyslist = [key for key in dictionary]\r\n# print the list of keys\r\nprint(keyslist)\r\n<\/pre>\n
['This', 'is', 'BTechGeeks']<\/pre>\n
\n