×

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 Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.

Best Database in Python

In this tutorial, Firstly, we will understand what the term database means. Further, we will see the various databases supported in Python and when to use which one. Further, we...

7 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.