LC 263 / LING 398
Elsayed Issa
Fall 2024

Dictionary Methods

Method Meaning
clear() appends an element to the end of the list.
copy() returns a copy of the specified dictionary.
items() returns the key-value pairs of the dictionary, as tuples in a list.
keys() returns the keys of the dictionary, as a list..
values() returns the values of the dictionary, as a list.
update() inserts an item or items to the dictionary.
fromkeys() returns a dictionary with the specified keys and the specified value.
get() returns the value of the item with the specified key
pop() removes the last key-value pair in a dictionary.
popitem() removes the item that was last inserted into the dictionary.
setdefault() returns the value of the item with the specified key.

clear()¶

  • removes all the elements from a dictionary.
In [1]:
purdue = {
    "location": "West Lafayette",
    "state": "Indiana",
    "founded": 1869
} 

purdue.clear()
print(purdue)
{}

copy()¶

  • returns a copy of the specified dictionary.
In [4]:
purdue = {
    "location": "West Lafayette",
    "state": "Indiana",
    "founded": 1869
}

x = purdue.copy()
x['location'] = 'Tucson'
print(x)

print(purdue)
{'location': 'Tucson', 'state': 'Indiana', 'founded': 1869}
{'location': 'West Lafayette', 'state': 'Indiana', 'founded': 1869}

items()¶

  • returns the key-value pairs of the dictionary, as tuples in a list.
In [6]:
purdue = {
    "location": "West Lafayette",
    "state": "Indiana",
    "founded": 1869
}

x = purdue.items()

print(x)

for item in purdue.items():
    print(item)
dict_items([('location', 'West Lafayette'), ('state', 'Indiana'), ('founded', 1869)])
('location', 'West Lafayette')
('state', 'Indiana')
('founded', 1869)
In [ ]:
# here we changed the year from 1869 to 2023
purdue = {
    "location": "West Lafayette",
    "state": "Indiana",
    "founded": 1869
}

x = purdue.items()

purdue["founded"] = 2023

print(x)

keys()¶

  • returns the keys of the dictionary as a list.
In [ ]:
info = {
  "vowels": "aeiou",
  "consonants": "abcdfghjklmnpqrstvwxyz",
}

x = info.keys()
print(x)
In [ ]:
info = {
  "vowels": "aeiou",
  "consonants": "abcdfghjklmnpqrstvwxyz",
}

x = info.keys()

info["language"] = "English"

print(x)

values()¶

  • returns the values of the dictionary as a list.
In [ ]:
info = {
  "vowels": "aeiou",
  "consonants": "abcdfghjklmnpqrstvwxyz",
}

x = info.values()
print(x)
In [ ]:
info = {
  "vowels": "aeiou",
  "consonants": "abcdfghjklmnpqrstvwxyz",
}

x = info.values()

info["language"] = "English"

print(x)

update(iterable)¶

  • inserts any items to the dictionary. It takes an argument: iterable (a dictionary or an iterable with key and value)
In [ ]:
info = {
  "vowels": "aeiou",
  "consonants": "abcdfghjklmnpqrstvwxyz",
}

info.update({"language": "English"})

print(info)

fromkeys(keys, value)¶

  • returns a dictionary with the specified keys and the specified value. It takes two arguments: keys (required) and value (optional).
In [ ]:
k = ['1', '2', '3']
v = "FFFF"

d = dict.fromkeys(k, v)
print(d)

get(keyname, value)¶

  • returns the value of the item with the specified key. It takes two arguments: keyname (required) and value (optional).
In [ ]:
sentence = {
  "pronoun": "I",
  "verb": "love",
  "propernoun": "python"
}

x = sentence.get("verb")

print(x)

pop(keyname, defaultvalue)¶

  • removes the last key-value pair in a dictionary. It takes two arguments: keyname (required) and defaultvalue (optional).
In [ ]:
sentence = {
  "pronoun": "I",
  "verb": "love",
  "propernoun": "python"
}

x = sentence.pop("pronoun")

sentence

pipitem()¶

  • removes the item that was last inserted into the dictionary.
In [ ]:
sentence = {
  "pronoun": "I",
  "verb": "love",
  "propernoun": "python"
}

sentence.popitem()

print(sentence)

setdefault(keyname, value)¶

  • returns the value of the item with a certain key. It has two arguments: keyname (required) and value (optional)
In [ ]:
car = {
  "brand": "Honda",
  "model": "Accord",
  "color": "Gold"
}

res = car.setdefault("model")

res
In [ ]: