Change language

Python Dictionary | keys () method

|

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

Syntax : dict.keys ()

Parameters: There are no parameters.

Returns: A view object is returned that displays all the keys. This view object changes according to the changes in the dictionary.

Example # 1:

# Python program to show the work
Number of keys in the dictionary

  
# Three-key dictionary

Dictionary1 = { ’ A’ : ’Geeks’ , ’ B’ : ’For’ , ’ C’ : ’Geeks’ }

  
  # Printing dictionary keys

print (Dictionary1.keys () )

 
# Create an empty dictionary

empty_Dict1 = {}

 
# Print empty dictionary keys

print (empty_Dict1.keys ())

Exit :

 dict_keys ([’A’,’ B’, ’C’]) dict_keys ([]) 

The order of the values ​​of these keys in the list may not always be the same.

Example # 2: To show how updating a dictionary works

# Python program ma to show updates
Number of keys in the dictionary

 
# Dictionary with two keys

Dictionary1 = { ’A’ : ’Geeks’ , ’ B’ : ’For’ }

  
# Print dictionary keys

print ( "Keys before Dictionary Updation:" )

keys = Dictionary1.keys ()

print ( keys)

 
# add an item to the dictionary

Dictionary1.update ({ ’C’ : ’Geeks’ })

  

print ( ’After dictionary is updated:’ )

print (keys)

Exit:

 Keys before Dictionary Updation: dict_keys ([’B’,’ A’]) After dictionary is updated: dict_keys ([’B’,’ A’, ’C’]) 

Here, when the dictionary is updated, the keys are also automatically updated to show changes.

Practical use: keys () can be used to access dictionary elements, as we can do for a list, without using the () keys, no other mechanism provides a means to access the dictionary keys as a list by index. This is demonstrated in the example below.

Example # 3: Demonstration of practical use of () keys

# Program Python for demonstration
# key () operation

 
# initializing dictionary

test_dict = { "geeks" : 7 , "for" : 1 , " geeks " : 2 }

 
# access the second element using a naive method
# using a loop

j = 0

for i in test_dict:

if (j = = 1 ):

print ( ’2nd key using loop : ’ + i)

  j = j + 1

 

 
# access to the second element using () keys

print ( ’2nd key using keys ():’ + test_dict.keys () [ 1 ])

Output:

 2nd key using loop: for 2nd key using keys (): for 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method