Can Python list dict keys?
Show
In this article we will discuss how to create a list of all keys in a dictionary. Suppose we have a dictionary of string and integers i.e. Creating a list of all keys in dictionary using dict.keys()In python, dictionary class provides a member function i.e. Creating a filtered list of dictionary keys using List ComprehensionSuppose from above mentioned dictionary, we want a list of keys that start with character ‘t’ only i.e. Python Dictionary Tutorial - Series:Complete example is as follows, Output: Dictionary : {'city': 2, 'test': 43, 'at': 23, 'Hello': 56, 'this': 78, 'here': 18} List of keys in Dictionary : ['city', 'test', 'at', 'Hello', 'this', 'here'] List of keys in Dictionary that start with 't' : ['test', 'this'] List of keys in Dictionary that start with 't' : ['test', 'this']
In this article we will discuss different ways to convert a single or multiple lists to dictionary in Python. Following conversions from list to dictionary will be covered here,
Convert a List to Dictionary with same valuesSuppose we have a list of strings i.e. Now we want to create a dictionary with all elements in this list as keys. For each key value will be same i.e. 5. Let’s see how to do that i.e. Using dictionary comprehension Using dict.fromKeys() ''' Converting a list to dictionary with list elements as keys in dictionary using dict.fromkeys() ''' dictOfWords = dict.fromkeys(listOfStr , 1) dict.fromKeys() accepts a list and default value. It returns a dictionary with items in list as keys. All dictionary items will have same value, that was passed in fromkeys(). If no default value was passed in fromKeys() then default value for keys in dictionary will be None. Convert List items as keys in dictionary with enumerated valueSuppose we have a list of strings i.e. Convert two lists to a dictionarySuppose we have two lists i.e. We have passed two lists objects in zip() , so it will return a list of tuples, where each tuple contains an entry from both the lists. Then we created a dictionary object from this list of tuples. Dictionary contents are,
Convert a list of tuples to dictionarySuppose we have a list of tuples with two columns in each entry i.e. Python Dictionary Tutorial - Series:Complete example is as follows, Output ************* Dictionary with same value now :: 5 here :: 5 test :: 5 at :: 5 this :: 5 hello :: 5 ************* Dictionary with given default value now :: 1 here :: 1 test :: 1 at :: 1 this :: 1 hello :: 1 ************* Dictionary with same default value None now :: None here :: None test :: None at :: None this :: None hello :: None ************* Dictionary with enumerated keys 0 :: hello 1 :: at 2 :: test 3 :: this 4 :: here 5 :: now ************* Dictionary from two Lists now :: 102 here :: 43 test :: 43 at :: 23 this :: 97 hello :: 56 ************* Dictionary from two Lists test :: 43 at :: 23 this :: 97 hello :: 56 here :: 43 ************* Dictionary from List of Tuples John :: 22 Lucy :: 90 Riti :: 11 Aadi :: 12 Sam :: 13 |