Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 Dictionary items() Method

Python3 Dictionary items() Method

Python3 Dictionaries Python3 Dictionary


Description

The Python dictionary's items() method retrieves all key/value pairs from a dictionary and returns a dict_items view object.

Each element in the view object is a tuple of two values, in the form (key, value), representing the dictionary's key and value respectively.

For example:

dict_items([('Name', 'Runoob'), ('Age', 7)])

items() The return value is not a list but a dynamic view. When the dictionary's elements change, the view object reflects those changes.

The difference between keys() and values() In the same way,items() The returned view object supports iteration but does not support direct access to elements by index.

If index access is needed, you can first use list() to convert the view into a list.

Syntax

items() method syntax:

dict.items()

Parameters

none.

Return Value

Returns a dict_items A view object containing all key/value pairs in the dictionary.

This object has the following characteristics:

Starting with Python 3.7, dictionary insertion order is part of the language specification, so items() iterating over key/value pairs preserves the dictionary's insertion order.

Example

The following example shows items() the basic usage of the method:

Example

#!/usr/bin/python3

tinydict

= {

'

Name

'

:

'

Runoob

'

,

'

Age

'

:

7

}

print

(

"

Value : %s

"

%

tinydict

.

items

(

)

)

The output of the above example is:

Value : dict_items([('Name', 'Runoob'), ('Age', 7)])

As you can see, the result contains two key/value pairs, each represented as a tuple.

Example 2: iterating over the dictionary's keys and values

items() The most common use is together with a for loop, obtaining both the keys and values of the dictionary at once.

Example

tinydict

= {

'

Name

'

:

'

Runoob

'

,

'

Age

'

:

7

,

'

Site

'

:

'

www.runoob.com

'

}

for

key

,

value

in

tinydict

.

items

(

)

:

print

(

key

,

"

:

"

,

value

)

Execution result:

Name : Runoob
Age : 7
Site : www.runoob.com

Wherekey receives the dictionary's key,value receives the corresponding value. This approach uses Python's tuple unpacking.

Example 3: converting items() to a list

Because dict_items Indexing is not supported; if you need to access key/value pairs by subscript, convert it to a list first.

Example

tinydict

= {

'

Name

'

:

'

Runoob

'

,

'

Age

'

:

7

}

items_list

=

list

(

tinydict

.

items

(

)

)

print

(

items_list

)

print

(

items_list

[

0

]

)

print

(

items_list

[

1

]

)

Execution result:

[('Name', 'Runoob'), ('Age', 7)]
('Name', 'Runoob')
('Age', 7)

**Note:** the converted list is a snapshot at the time of conversion; it does not update automatically when key/value pairs are added to or removed from the original dictionary.

Example 4: dynamic view

items() The returned view remains associated with the original dictionary.

After modifying values or adding/removing key/value pairs, looking at the same view object again shows the updated contents.

Example

tinydict

= {

'

Name

'

:

'

Runoob

'

,

'

Age

'

:

7

}

Get the view object

items_view

=

tinydict

.

items

(

)

print

(

items_view

)

Modify a value in the dictionary

tinydict

[

'

Age

'

]

=

8

Add a new key/value pair

tinydict

[

'

Site

'

]

=

'

www.runoob.com

'

print

(

items_view

)

Execution result:

dict_items([('Name', 'Runoob'), ('Age', 7)])
dict_items([('Name', 'Runoob'), ('Age', 8), ('Site', 'www.runoob.com')])

As you can see, even though it was called only once, items(), the contents shown by the view object are updated when the original dictionary changes.

Note that you cannot add or remove dictionary entries directly through the view object. To modify the dictionary, operate on the dictionary itself.

Example 5: checking whether a key/value pair exists

items() The returned view also supports using the in operator to check whether a key/value pair exists.

Example

tinydict

= {

'

Name

'

:

'

Runoob

'

,

'

Age

'

:

7

}

print

(

(

'

Name

'

,

'

Runoob

'

)

in

tinydict

.

items

(

)

)

print

(

(

'

Age

'

,

8

)

in

tinydict

.

items

(

)

)

Execution result:

True
False

Both the key and the value must match. For example,('Age', 8) does not exist, because in the dictionary the 'Age' corresponding value is 7。

Notes

If you need to iterate over a dictionary and delete keys that meet a condition, create a list copy of the key/value pairs first:

Example

scores

= {

'

Tom

'

:

85

,

'

Jack

'

:

59

,

'

Lucy

'

:

92

}

Iterate over the copy so that modifying the original dictionary does not affect iteration

for

name

,

score

in

list

(

scores

.

items

(

)

)

:

if

score

<

60

:

del

scores

[

name

]

print

(

scores

)

Execution result:

{'Tom': 85, 'Lucy': 92}

This approach suits cases where dictionary entries must be removed based on their key/value pairs.


Python3 Dictionaries Python3 Dictionary