TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Dictionary items() method with Examples

Python Dictionary items() method with Examples on clear(), copy(), fromkeys(), get(), len(), items(), keys(), setdefault(), values(), ascii(), enumerate(), update(), pop() etc.

<< Back to PYTHON

Python Dictionary items() Method

Python item() method returns a new view of the dictionary. This view is collection of key value tuples. This method does not take any parameter and returns empty view if the dictionary is empty. The examples and syntax are given below.

Signature

items()

Parameters

No parameter

Return

It returns a dictionary's view.

Let's see some examples of items() method to understand it's functionality.

Python Dictionary items() Method Example 1

This is a simple example which returns all the items present in the dictionary.

# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Calling function
items = student.items()
# Displaying result
print(items)

Output:

dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])

Python Dictionary items() Method Example 2

If the dictionary is already empty, this method does not raise any error. See the example below.

# Python dictionary items() Method
# Creating a dictionary
student = {} # dictionary is empty
# Calling function
items = student.items()
# Displaying result
print(items)

Output:

dict_items([])

Python Dictionary items() Method Example 3

Apart from items() method, we can also use other customized approaches to fetch dictionary elements. See an example below.

# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Iterating using key and value
for st in student:
    print("(",st, ":", student[st], end="), ")
# Calling function    
items = student.items()
# Displaying result
print("\n", items)

Output:

( name : rohan), ( course : B.Tech), ( email : [email protected]), 
 dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])

Next TopicPython Dictionary




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf