×

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

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python PyMOTW

The cmd module comprises one class of the general public, Cmd, meant for command processors such interactive shells and other command interpreters as a foundation class. It utilises readline as...

3 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.