Python

Items() Method in Python

In Python, a dictionary is a list of elements that are not sorted. In the Python dictionary, every key is matched with its corresponding value just like in a real dictionary where every word is related to its explanation. A content that is kept in the dictionary under the combination of key values is considered an element. The information kept in the dictionary is not organized and is mutable. So, once the dictionary is created, the items can be modified. The elements, therefore, cannot be repeated inside a single dictionary. Any type of data is used for the dictionary’s elements. Lists, dictionaries, tuples, as well as the other data structures are all used to hold the data.

Parameters of the Items() Function

Every item in a dictionary is returned as a list of attribute values using the items() function in the dictionary. The dictionary key and value combinations would therefore be presented as a list of tuple pairings when they are returned.

Syntax of Items() Function

There are no arguments required for the items() function.

Return Value

The key and the value combination defined in a dictionary are shown as tuples in a list whenever we call the items() function on it. The returned set represents a view of the dictionary’s elements. A dictionary is not converted into a list using this technique. Additionally, the list display also indicates all modifications made to the dictionary.

In this article, we talk about the several methodologies used to acquire the items from the dictionary.

Example 1:
In this case, we utilize the items() function and return all the elements defined in the dictionary.

Dict_1 = { 'Z': 'Japan', 'T': 9, 'M': 'Paris' }

print("The dictionary values are:")
print(Dict_1.items())

The first step is to initialize a “Dict_1” variable. Here, we save some values which include the numerical values, alphabetic, and strings. They all are in the form of a dictionary data type. This dictionary has three elements. A dictionary is an unsorted representation of the data elements that can be utilized to save data items in Python. In contrast to the other data types which would only store a single item, the dictionaries can also hold a key:value combination. The items() function in the Python dictionary is applied to return a collection of all dictionary keys as well as the numeric values.

Now, we call the print method to print the statement “The dictionary values are:”. Along with this, we display the elements of the defined dictionary by using the print statement. Within the print() function, the items() method is applied.

The values mentioned in the list are not constantly seen in a similar order.

Example 2:
We demonstrate how the items() method operates with a dictionary update. The following sample illustrates how the view object changes as the dictionary becomes modified.

Dict = { 'F': 'Cricket', 'K': 2, 'P': 'Hockey' }
print("The actual dictionary values are:")
i = Dict.items()
print(i)
del[Dict['P']]
print('The modified dictionary values are:')
print(i)

The initialization of the “Dict” variable is the first step. We store some values including strings, alphabets, and numerical values. They are all expressed as dictionary data types. There are three components to this dictionary. Now, in order to print the phrase “The actual dictionary values are:”, we invoke the print function.

In addition, we utilize the print statement to exhibit the specified dictionary’s values. The items() function is used within the print() method. We want to make some changes to the required dictionary. We delete the key-value “P” so we use the del() method. Then, the print() function is applied to represent the line “The modified dictionary values are:”. After deleting the value, we print the residual elements of the dictionary.

Example 3:
The items() approach raises no exceptions if the dictionary has been empty. In this instance, we make a blank dictionary. Let’s see the following illustration:

games = {}

a = games.items()  
print(a)

We begin the code by creating a dictionary as well as declaring a variable named “games”. This dictionary is empty. We call the items() function that is used to store the values of the dictionary. In the end, we display the elements of the dictionary with the assistance of the print() method.

Example 4:
In this instance, some other specialized methods are used in addition to the items() function to get the dictionary items.

emp = {'emp_name':'Raheem', 'emp_status':'HR', 'emp_email':'[email protected]'}  
for x in emp:  
    print("(",x, ":", emp[x], end="), ")  
l = emp.items()  

print("\n", l)

First of all, we construct a dictionary termed “emp”. This dictionary stores the information of the employee. The dictionary includes the name of the employee, status of his job, and email-id of the employee. In the later step, we utilize the “for” loop. We initialize a loop variable “x”. This loop iterates over every key and value of the required dictionary and prints the values.

Next, we apply the items() method and all the elements of the dictionary are stored in a variable “l”. To represent the items of the dictionary, we employ the print() method at the end of the program.

Conclusion:

In this article, we examined the use of the items() method in Python. A dictionary attribute that gives a realistic representation of dictionary elements as a set of identified pairs is returned by the dict.items() method. When the dictionary is updated, this display object is also modified. The first example of this guide demonstrates the use of the items() method to get all the values of the dictionary. In the second illustration, we modified the elements of the dictionary by using the items() method. The third example showed us that when we apply the items() method on an empty set, it doesn’t raise an error. In the last instance, we utilized the “for” loop to get the items in the dictionary. Any data type, which includes the strings and numeric values, is used for the components of the dictionary.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content