Python items

The Python items method on dictionary is used to return the list with all the dictionary keys with values.

This method takes no parameters, so the syntax is very simple: d.items(), where d represents the dictionary.

The items() method, therefore, returns an object that displays a list of pairs of tuples (key, value) of a dictionary.

Python items – first example

In this first simple example we print the pairs of tuples (key, value) on the student dictionary, which contains the data of a student.


student = {
'name': 'Cristina', 'age': 20, 'mail': 'info@codingcreativo.it'
}

print(student.items())

The output will be represented by this object which contains the pairs of tuples:

dict_items([('name', 'Cristina'), ('age', 20), ('mail', 'info@codingcreativo.it')])

The order could also be different.

Python items – second example

Let’s try now with a dictionary whose values ​​are in turn dictionaries. The students dictionary which contains the data of 2 students.

We want to use Python’s items () method on the entire dictionary. What will the result be?


students = {
'1': {'name' : 'Cristina', 'age': 29, 'mail': 'info@codingcreativo.it'},
'2': {'name' : 'Tom', 'age': 23, 'mail': 'info@prova.it'}
}

print(students.items())

The output generated will be this:

dict_items([('1', {'name': 'Cristina', 'age': 29, 'mail': 'info@codingcreativo.it'}), ('2', {'name': 'Tom', 'age': 23, 'mail': 'info@prova.it'})])

Try the following code in the online compiler that you find at the following link: online Python compiler.

Python items – third example

Now we want to use the items method on internal dictionaries. How can we do?

In order to use the items method, for this purpose, we must first select the key whose pairs of tuples we want to obtain. Then we can apply the method.

Here is therefore a possible solution to the proposed algorithm:


students = {
'1': {'name' : 'Cristina', 'age': 29, 'mail': 'info@codingcreativo.it'},
'2': {'name' : 'Tom', 'age': 23, 'mail': 'info@prova.it'}
}

student = students.get('1', {}).items()
print(student)

In this case, the output generated will be this:

dict_items([('name', 'Cristina'), ('age', 29), ('mail', 'info@codingcreativo.it')])

This is if the dictionaries are the same, but what if the values ​​of keys 1 and 2 are represented by different dictionaries?


''' 
Example with different keys
'''
students = {
'1': {'name' : 'Cristina', 'age': 29, 'mail': 'info@codingcreativo.it'},
'2': {'exam' : 'English', 'vote': 30}
}

for student in students.values():
  print(student.items())

In this case, the output will be the following:

dict_items([('name', 'Cristina'), ('age', 29), ('mail', 'info@codingcreativo.it')])
dict_items([('exam', 'English'), ('vote', 30)])

Conclusion

In this lesson we have made some simple examples of using the items () method in Python on dictionaries. In the next lesson we will see what happens if we delete or add elements to a dictionary.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

Python

Bubble sort

Matplotlib Plot

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *