Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Dictionary keys() method
Next article icon

Python Dictionary items() method

Last Updated : 26 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.

Example:

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}  # using items() to get all key-value pairs items = d.items()  print(items) 

Output
dict_items([('A', 'Python'), ('B', 'Java'), ('C', 'C++')]) 

Explanation: items() method returns a dict_items view object containing all key-value pairs in the form of tuples.

Note : items() method is only available for dictionary objects. If called on a non-dictionary object, it will raise an AttributeError.

items() syntax

dict.items()

Here, dict is the dictionary from which the key-value pairs are retrieved.

Parameters:

  • items() method does not take any parameters.

Return value:

  • This method returns a dict_items view object, which behaves like a list of (key, value) tuples. If the dictionary is updated, the view object automatically reflects these changes.

Examples of items()

Example 1: Iterating over key-value pairs

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}  # using items() to iterate over dictionary for key, value in d.items():     print(f"Key: {key}, Value: {value}") 

Output
Key: A, Value: Python Key: B, Value: Java Key: C, Value: C++ 

Explanation: items() method allows easy unpacking of key-value pairs using tuple unpacking inside a loop.

Example 2: Dynamic nature of items()

Python
d = {'A': 'Python', 'B': 'Java'}  # getting items items = d.items()  # adding a new key-value pair d['C'] = 'C++'  print(items) 

Output
dict_items([('A', 'Python'), ('B', 'Java'), ('C', 'C++')]) 

Explanation: When a new key-value pair is added to the dictionary, the items() view object updates automatically to include the new pair.

Example 3: Converting items() to a list

Python
d = {'A': 'Python', 'B': 'Java', 'C': 'C++'}  # converting items to a list items_list = list(d.items())  print(items_list) 

Output
[('A', 'Python'), ('B', 'Java'), ('C', 'C++')] 

Explanation: We convert the dict_items object into a list of tuples, allowing for indexing, sorting or other list operations.


Next Article
Python Dictionary keys() method

A

Akanksha_Rai
Improve
Article Tags :
  • Python
  • python-dict
  • Python-dict-functions
Practice Tags :
  • python
  • python-dict

Similar Reads

    Python Dictionary Methods
    Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
    5 min read
    Python Dictionary clear()
    clear() method in Python is used to remove all items (key-value pairs) from a dictionary. After calling this method, the dictionary will become empty and its length will be 0. This method is part of the built-in dictionary operations in Python.Example:Pythond = {1: "geeks", 2: "for"} # using clear()
    2 min read
    Python Dictionary copy()
    Python Dictionary copy() method returns a shallow copy of the dictionary. let's see the Python Dictionary copy() method with examples:  Examples Input: original = {1:'geeks', 2:'for'} new = original.copy() // Operation Output: original: {1: 'one', 2: 'two'} new: {1: 'one', 2: 'two'}Syntax of copy()
    3 min read
    Python Dictionary fromkeys() Method
    Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value. Python Dictionary fromkeys() Method Syntax: Syntax : fromkeys(seq, val) Parameters : seq : The sequence to be transformed into
    3 min read
    Python Dictionary get() Method
    Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).Python Dictionary get() Method Syntax:Syntax : Dict.get(key, Value)Parameters: key: The key name of the item you want to return
    3 min read
    Python Dictionary items() method
    items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
    2 min read
    Python Dictionary keys() method
    keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge
    2 min read
    Python Dictionary pop() Method
    The Python pop() method removes and returns the value of a specified key from a dictionary. If the key isn't found, you can provide a default value to return instead of raising an error. Example:Pythond = {'a': 1, 'b': 2, 'c': 3} v = d.pop('b') print(v) v = d.pop('d', 'Not Found') print(v) Output2 N
    2 min read
    Python Dictionary popitem() method
    popitem() method in Python is used to remove and return the last key-value pair from the dictionary. It is often used when we want to remove a random element, particularly when the dictionary is not ordered. Example:Pythond = {1: '001', 2: '010', 3: '011'} # Using popitem() to remove and return the
    2 min read
    Python Dictionary setdefault() Method
    Python Dictionary setdefault() returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to the dictionary. Python Dictionary setdefault() Method Syntax: Syntax: dict.setdefault(key, default_value)Parameters: It takes two parameters: key - Key to be sear
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences