Monity dictionary8/3/2023 We print out the returned objects for both the dictionaries. In the above example, the keys() function is used to get the keys of dictionaries shares1 and shares2. Print("Keys of shares2:", shares2.keys()) Print("Keys of shares1:", shares1.keys()) # print the keys of the shares1 and shares2 For more on dictionary view objects, refer to the python docs.Įxample 1: Using the keys() function to display a dictionary’s keys # dictionary of a sample portfolio Returns: It returns a view object of the dictionary’s keys. Parameters: The keys() function does not take any parameters. Here, sample_dict is the dictionary whose keys you want to get. The following is the syntax: sample_dict.keys() The python dictionary keys() function gives a view object that displays a list of all the keys in the dictionary. They return a view object that provides a dynamic view of the dictionary’s entries, meaning when the dictionary changes, the view reflects these changes. The dictionary functions keys() and values() can be used to view a dictionary’s keys and values. View Keys and Values of a python dictionary For more, check out our guide on dictionaries and other data structures in python. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. In this tutorial, we’ll look at how to use these functions along with some examples.īefore we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. In python, we can view a dictionary’s keys and values using the dictionary functions keys() and values() respectively.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |