Python Dictionary Methods with Examples

Python dictionary is a popular type used to store single or multiple key-value pairs in an array or list-like structure. Python dictionary consists of items where every item has a key and related value. The Key is mainly used to access, set, update the value. It is like and language dictionary to translate the English term into the Turkish term. In this tutorial, we will learn different useful dictionary methods useful to work with a Python dictionary.

Create Python Example Dictionary

Before starting to examine dictironary methods or functions we will create a sample dictionary in order to work with these functions.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

clear() – Remove All Items

clear() method is used to clear, remove or delete all items inside given dictionary. It has very simple syntax where there is no parameter. When called all the dictionary items will be cleared.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

students.clear()

print(students)
clear() – Remove All Items

copy() – Return Copy of Dictionary

The copy() method returns the complete copy of the given dictironary. This can be used to copy or clone existing dictionary and its data into a new dictionary. The syntax of the copy() method is very simple which do not requires any parameter.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

students_clone = students.copy()

print(students_clone)
copy() – Return Copy of Dictionary

fromkeys() – Return Specified Key and Values

fromkeys() method is used to create new dictionary with specified keys. Also values can be specified for these keys but values are optional and if not specified all values will be 0 by default. fromkeys() method has the following syntax.

dict.fromkeys( keys , value )
  • fromkeys() method will return a dictionary with specified keys and values.
  • keys are a tuple which provides the keys. keys should be provided and not optional.
  • value can be a single value that will be assigned into all keys and value is optional and if not provided the keys values will be None.

Below we will make different examples about the fromkeys() method.

keys = ( "ahmet" , "ali" , "elif" , "ecrin" )

value = 1

d1 = dict.fromkeys( keys , value )

print(d1)


d2 = dict.fromkeys( keys , 5 )

print(d2)


d2 = dict.fromkeys( keys )

print(d2)

get() – Return Value of Specified Key

get() method is used to return the specified key value in a dictionary. Alternatively, if the specified key does not exist a specified value can be returned. get() method has the following syntax.

get( key , value )
  • key is the key we want to get its value. The key parameter is required.
  • value is the return value if the specified key is not found in the dictionary. The value parameter is optional.
students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

v1 = students.get("ahmet")
print(v1)


v2 = students.get("elif")
print(v2)


v3 = students.get("elif" , -1)
print(v3)


v4 = students.get("ismail" , -1)
print(v4)
get() – Return Value of Specified Key

items() – Return Items as Tuple

items() method returns all dictionary key/value pair views. The view is used to access the key/value pairs but can not change the original dictionary. When one of the key-value changes the view will provide the changed value. items() returns the key/value pairs as tuple type.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

v = students.items()

print(v)

students["ahmet"] = 121212

print(v)
items() – Return Items as Tuple

keys() – Return Only Keys As List

keys() method is similar to the items where keys() method return only the keys of the given dictionary as a list. Any later change on the dictionary keys will reflect the returned list.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

l = students.keys()

print(l)

students["veli"] = 25

print(l)
keys() – Return Only Keys As List

pop() – Remove Specified Item

pop() method will remove the specified key/value pair from the dictionary by returning this key/value pair. pop() method requires a key-value to select the key/value pair if the specified key does not exist a default value can be provided to return as key/value pair. The syntax of the dictionary pop() method is like below.

pop(key,value)
  • key is the item key we want to remove from the dictionary and return for pop() method with its value.
  • value is the returned value of the key/value pair if the specified key does not exist in the specified dictionary.
students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }



item1 = students.pop( "ahmet" )

print(item1)

print(students)



item2 = students.pop( "mehmet" , -1)

print(item2)
pop() – Remove Items

popitem() – Remove Last Inserted Item

popitem() method is similar to the pop() method where there is no parameter. popitem() removes last inserted item or key/value pair and returns this key/value pair. The removed key/value pair is returned as a tuple type. It has the following simple syntax.

popitem()
students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }


item = students.popitem()

print(item)


item = students.popitem()

print(item)


print(students)
popitem() – Remove Last Inserted Item

setdefault() – Return Value of Specified Key If Not Exist Insert Key-Value

setdefault() method returns the value of the specified key. If the key do not exist it will add the key with a provided value. The syntax of the setdefault() method is like below.

setdefault( key , value )
  • key specifies the key of the key/value pair where the value will return.
  • value specifies if the specified key does not exist in the dictionary the key will be added with the specified value. value is optional and if not specified and the key does not exist None value will be added with the key.
students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }


value = students.setdefault( "ahmet" )
print(value)


value = students.setdefault( "ahmet" , 1212 )
print(value)


value = students.setdefault( "veli" , 1212 )
print(value)


value = students.setdefault( "ismail" )
print(value)
setdefault() – Return Value of Specified Key If Not Exist Insert Key-Value

update() – Update/Add Dictionary with Specified Key-Value

update() method updates or adds the specified iterable value into the dictionary. The iterable value can be a dictionary or a list. The syntax of the update() method is like below.

update(iterable)
  • iterable is a iterable value like dictionary.
students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

students.update( { "veli":22 } )

print(students)
update() – Update/Add Dictionary with Specified Key-Value

values() – Return All Values of Dictionary

The values() method returns a view object. This view object contains the values of the dictionary as a list. If the dictionary changes the object view also changes too. The syntax of the values() method is very simple where there is no a parameter. The returned data is a list of the dictionary values.

students = { "ahmet":12 , "ali":5 , "elif":3 , "ecrin":10 }

l = students.values()

print(l)


students["veli"] = 100

print(l)
values() – Return All Values of Dictionary

Leave a Comment