Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
109 views

08 Python Dictionary Methods

This document describes various dictionary methods in Python including clear(), copy(), fromkeys(), get(), items(), keys(), pop(), popitem(), setdefault(), update(), and values(). Each method is explained with its syntax, parameters, and an example showing how it can be used.

Uploaded by

Thohith Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

08 Python Dictionary Methods

This document describes various dictionary methods in Python including clear(), copy(), fromkeys(), get(), items(), keys(), pop(), popitem(), setdefault(), update(), and values(). Each method is explained with its syntax, parameters, and an example showing how it can be used.

Uploaded by

Thohith Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Dict Methods

clear()
Removes all the elements from a dictionary.
Syntax – dictionary.clear()

Ex:
student = {
"name": "Krishna",
"age": 25,
"place": "Vrindavan"
}

student.clear()
print(student)
copy()
Returns a copy of the specified dictionary.
Syntax – dictionary.copy()

Ex:
god = {
"name": "Krishna",
"age": 25,
"place": "Vrindavan"
}

x = god.copy()
print(x)
fromkeys()
Returns a dictionary with the specified keys
and the specified value.

Ex:
x = ('ssc', 'inter', 'btech')
y = 'Anantapur'

student_dict = dict.fromkeys(x, y)

print(student_dict)
Syntax – dict.fromkeys(keys, value)

Parameters:
keys – Required – iterable
An iterable specifying the keys of the
new dictionary.

value – Optional – value


The value for all keys.
Default value is None.
get()

Returns the value for the specified key if


the key is in dictionary.

Returns None if the key is not found and


value is not specified.

Returns the value if the key is not found


and value is specified.
Syntax – dictionary.get(key, value)

Parameters:
key – Required – key
The key name.

value – Optional – value


The value to return if the specified key does
not exist.

Default value is None.


items()
Returns a view object.

The view object contains the key-value pairs of the


dictionary, as tuples in a list.

The objects returned by the dictionary methods like


keys(), values(), items() are called as view objects.

The view object will reflect any changes done to the


dictionary, as shown in below example.

Syntax – dictionary.items()
Parameters: No
student = {
"name": "Hari",
"age":25,
"email": "hari@gmail.com"
}

x = student.items()
print(x)

student["age"] = 30
print(x)
keys()
Returns a view object.

View object contains the key of the dictionary, as a list.

The objects returned by the dictionary methods like


keys(), values(), items() are called as view objects.

The view object will reflect any changes done to the


dictionary.

Syntax – dictionary.keys()

Parameters: No
pop()
Updates the dictionary by removing the specified item
from.

Returns the removed dictionary item.

Syntax – dictionary.pop(key, value)

Parameters:
key – Required – key
The key name.

value – Optional – value


The value to return if the specified key does not exist.
Default value is None.
popitem()
Removes the item that was last inserted into
the dictionary.

In versions before 3.7, this method was used


to remove a random item.

Returns the removed item as a tuple.

Syntax – dictionary.popitem()

Parameters: No
setdefault()

Returns the value of the item with


the specified key.

If the key does not exist, inserts the


key with the specified value into the
dictionary.
Syntax – dictionary.setdefault(key, value)

Parameters:
key – Required – key
The key name.

value – Optional – value


If the key exist, this parameter has no effect.

If the key does not exist, this value becomes the


key's value.

Default value is None.


update()
Updates the specified key with the specified value.

Adds items to the dictionary, if the specified key is


not in the dictionary.

Syntax:dictionary.update(dictionary)

Parameters:
dictionary – Required – dictionary
A dictionary with key and value.
values()
Returns a view object.

View object contains the value of the dictionary, as a list.

The objects returned by the dictionary methods like


keys(), values(), items() are called as view objects.

The view object will reflect any changes done to the


dictionary.

Syntax – dictionary.values()

Parameters: No

You might also like