×

Python Dictionary keys() method

Python Dictionary keys() method

The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary

Syntax

dictionary.keys()

Parameter

NA

Return

This method returns a view object that displays a list of all the keys in the dictionary

Example 1

 # the python program representing
 # the dictionary.keys() method
 # initializing the dictionary
 fruits = {
  "banana": "apple",
   "orange": "mango",
   "grapes": 5
 }
 # printing the Dictionary
 print("Dictionary:",fruits)
 # returning a view object that displays a list of all the keys in the dictionary
 keysItem= fruits.keys()
 # printing the keys 
 print("The items in the dictionary are:",keysItem) 

Output

Dictionary: {'grapes': 5, 'orange': 'mango', 'banana': 'apple'}
The items in the dictionary are: dict_keys(['grapes', 'orange', 'banana'])

Example 2

 # the python program representing
 # the dictionary.keys() method
 # initializing the week dictionary
 week = { 'Monday': 1, 'Tuesday': 2, 'wednesday': 3 ,'Thursday':4,'Friday':5}
 items = week.keys()
 print('Original week list:', items)
 # deleting an item from the week dictionary
 del[week['Thursday']]
 # returning a view object that displays a list of all the keys in the dictionary
 print('Updated week list:', items) 

Output

Original week list: dict_keys(['Tuesday', 'Friday', 'Monday', 'Thursday', 'wednesday'])
Updated week list: dict_keys(['Tuesday', 'Friday', 'Monday', 'wednesday'])

Example 3

 # the python program representing
 # the dictionary.keys() method
 # intializing Dictionary with two keys 
 Dictionary = {'1. ': 'Tutorials', '2. ': 'And'} 
 # Printing the keys of dictionary 
 print("Keys before Dictionary Updation:") 
 keys = Dictionary.keys() 
 print(keys) 
 # adding an element to the dictionary 
 Dictionary.update({'3.':'Examples'}) 
 print('\nAfter updating the dictionary...') 
 print(keys) 

Output

 Keys before Dictionary Updation:
 dict_keys(['2. ', '1. '])
 After updating the dictionary...
 dict_keys(['2. ', '1. ', '3.']) 

Related Topics

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.