Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.
In Python Dictionary, items() method is used to return the list with all dictionary keys with values.
Syntax: dictionary.items()
Parameters: This method takes no parameters.
Returns: A view object that displays a list of a given dictionary’s (key, value) tuple pair.
Example #1:
Python3
Dictionary1 = { 'A' : 'Geeks' , 'B' : 4 , 'C' : 'Geeks' }
print ( "Dictionary items:" )
print (Dictionary1.items())
|
Output:
Dictionary items:
dict_items([('A', 'Geeks'), ('B', 4), ('C', 'Geeks')])
Order of these items in the list may not always be same.
Example #2: To show working of items() after modification of Dictionary.
Python3
Dictionary1 = { 'A' : 'Geeks' , 'B' : 4 , 'C' : 'Geeks' }
print ( "Original Dictionary items:" )
items = Dictionary1.items()
print (items)
del [Dictionary1[ 'C' ]]
print ( 'Updated Dictionary:' )
print (items)
|
Output:
Original Dictionary items:
dict_items([('A', 'Geeks'), ('C', 'Geeks'), ('B', 4)])
Updated Dictionary:
dict_items([('A', 'Geeks'), ('B', 4)])
If the Dictionary is updated anytime, the changes are reflected in the view object automatically.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Dec, 2020
Like Article
Save Article