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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically