×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Dictionary items() Method

By IncludeHelp Last updated : December 21, 2024

Python Dictionary items() Method

The items() is an inbuilt method of dict class that is used to get all items of the dictionary as a view object, the view object represents the key-value pair of the dictionary. The method is called with this dictionary and returns the dictionary items as a view object.

Syntax

The following is the syntax of items() method:

dictionary_name.items()

Parameter(s):

The following are the parameter(s):

  • None - It does not accept any parameter.

Return Value

The return type of this method is <class 'dict_items'>, it returns the items of the dictionary as view object.

Example 1: Use of Dictionary items() Method

# dictionary declaration
student = {
    "roll_no": 101, 
    "name": "Shivang", 
    "course": "B.Tech", 
    "perc": 98.5
}

# printing dictionary
print("data of student dictionary...")
print(student)

# printing items
print("items of student dictionary...")
print(student.items())

# printing return type of student.items() Method
print("return type is: ", type(student.items()))

Output

data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5}
items of student dictionary...
dict_items([('roll_no', 101), ('name', 'Shivang'), ('course', 'B.Tech'), ('perc', 98.5)])
return type is:  <class 'dict_items'>

Example 2: Use of Dictionary items() Method

# dictionary declaration
data = {"a": 1, "c": 3, "b": 5, "d": 4}

# printing original dictionary
print("Original Dictionary...")
print(data)

# printing items of the dictionary
print("Items of the dictionary...")
print(data.items())

Output

Original Dictionary...
{'a': 1, 'c': 3, 'b': 5, 'd': 4}
Items of the dictionary...
dict_items([('a', 1), ('c', 3), ('b', 5), ('d', 4)])

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.