Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Python Dictionary clear()

The clear() method removes all items from the


dictionary.
The syntax of clear() is:
dict.clear()
clear() method doesn't take any parameters.
clear() method doesn't return any value (returns None).

d = {1: "one", 2: "two"}


d.clear()
print('d =', d)

output
d = {}
Python Dictionary copy()
They copy() method returns a copy (shallow copy)
of the dictionary.
The syntax of copy() is:
dict.copy()
The copy() method doesn't take any arguments.
This method returns a shallow copy of the dictionary. It
doesn't modify the original dictionary.

original = {1:'one', 2:'two'}


new = original.copy()
print('Orignal: ', original)
print('New: ', new)
output
Orignal: {1: 'one', 2: 'two'}
New: {1: 'one', 2: 'two'}
Python Dictionary fromkeys()
The fromkeys() method creates a new dictionary
from the given sequence of elements with a value provided
by the user.
The syntax of copy() is:
dictionary.fromkeys(sequence[, value])
fromkeys() method takes two parameters:
sequence - sequence of elements which is to be
used as keys for the new dictionary
value (Optional) - value which is set to each each
element of the dictionary
fromkeys() method returns a new dictionary with the given
sequence of elements as the keys of the dictionary.
keys = {'a', 'e', 'i', 'o', 'u' }
value = [1]

vowels = dict.fromkeys(keys, value)


print(vowels)

# updating the value


value.append(2)
print(vowels)
Output

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}
{'a': [1, 2], 'u': [1, 2], 'o': [1, 2], 'e': [1, 2], 'i': [1, 2]}
Python Dictionary get()
The get() method returns the value for the
specified key if the key is in the dictionary.
The syntax of copy() is:
dict.get(key[, value])
get() method takes maximum of two parameters:
key - key to be searched in the dictionary
value (optional) - Value to be returned if the key is not
found.
get() method returns:
the value for the specified key if key is in the dictionary.
None if the key is not found and value is not specified.
value if the key is not found and value is specified.
person = {'name': 'Phill', 'age': 22}

print('Name: ', person.get('name'))

print('Age: ', person.get('age'))

# value is not provided


print('Salary: ', person.get('salary'))

# value is provided
print('Salary: ', person.get('salary', 0.0))
person = {'name': 'Phill', 'age': 22}

print('Name: ', person.get('name'))

print('Age: ', person.get('age'))

# value is not provided


print('Salary: ', person.get('salary'))

# value is provided
print('Salary: ', person.get('salary', 0.0))
Output

Name: Phill
Age: 22
Salary: None
Salary: 0.0
Python Dictionary items()
The items() method returns a view object that
displays a list of dictionary's (key, value) tuple pairs.
The syntax of copy() is:
dictionary.items()
The items() method doesn't take any parameters.
The items() method returns a view object that displays a list
of a given dictionary's (key, value) tuple pair.

sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }


print(sales.items())

Output
dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])
Python Dictionary keys()
The keys() method returns a view object that displays a list
of all the keys in the dictionary
The syntax of copy() is:
dict.keys()
doesn't take any parameters.
keys() returns a view object that displays a list of all the keys.

sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }


print(sales.keys())

Output
dict_keys(['apple','orange','grapes'])
Python Dictionary values()
The values() method returns a view object that displays a
list of all the values in the dictionary.
The syntax of copy() is:
dict. values()
values() method doesn't take any parameters.
values() method returns a view object that displays a list of all values
in a given dictionary.

marks = {'Physics':67, 'Maths':87}


print(marks.values())

Output
dict_values([67, 87])
Python Dictionary pop()
The pop() method removes and returns an element from a
dictionary having the given key.
The syntax of pop() is:
dict.pop(key[, default])
pop() method takes two parameters:
• key - key which is to be searched for removal
• default - value which is to be returned when the key is not in the
dictionary
The pop() method returns:
• If key is found - removed/popped element from the dictionary
• If key is not found - value specified as the second argument (default)
• If key is not found and default argument is not specified - KeyError
exception is raised
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
element = sales.pop('apple')
print('The popped element is:', element)
print('The dictionary is:', sales)

Output
The popped element is: 2
The dictionary is: {'orange': 3, 'grapes': 4}
Python Dictionary popitem()
The Python popitem() method removes and returns the last
element (key, value) pair inserted into the dictionary.
The syntax of popitem() is:
dict. popitem()
The popitem() doesn't take any parameters.
The popitem() method removes and returns the (key, value) pair
from the dictionary in the Last In, First Out (LIFO) order.

person = {'name': 'Phill', 'age': 22, 'salary': 3500.0}


result = person.popitem()
print('Return Value = ', result)
print('person = ', person)
Output
Return Value = ('salary', 3500.0)
person = {'name': 'Phill', 'age': 22}
Python Dictionary update()
The update() method updates the dictionary with the
elements from another dictionary object or from an iterable of
key/value pairs.
The syntax of update() is:
dict. update([other])
• The update() method takes either a dictionary or an iterable
object of key/value pairs (generally tuples). If update() is called
without passing parameters, the dictionary remains unchanged.
• It doesn't return any value (returns None)..

marks = {'Physics':67, 'Maths':87}


internal_marks = {'Practical':48}
marks.update(internal_marks)
print(marks)
Output
{'Physics': 67, 'Maths': 87, 'Practical': 48}
Python Dictionary setdefault()
The setdefault() method returns the value of a key (if the
key is in dictionary). If not, it inserts key with a value to the
dictionary.
The syntax of setdefault() is:
dict. setdefault(key[, default_value])

setdefault() takes a maximum of two parameters:


• key - the key to be searched in the dictionary
• default_value (optional) - key with a value default_value is
inserted to the dictionary if the key is not in the dictionary. If not
provided, the default_value will be None.

setdefault() returns:
• value of the key if it is in the dictionary
• None if the key is not in the dictionary and default_value is not
specified
• default_value if key is not in the dictionary and default_value is
specified
person = {'name': 'Phill', 'age': 22}
age = person.setdefault('age')
print('person = ',person)
print('Age = ',age)
sex= person.setdefault('sex', 'male')
city=person.setdefault('city')
print('Sex= ',sex)
print('City= ',city)

Output
person = {'name': 'Phill', 'age': 22}
Age = 22
Sex= male
City= None
PYTHON SETS
A set is an unordered collection of items. Every set element
is unique (no duplicates) and must be immutable (cannot be
changed).
However, a set itself is mutable. We can add or remove items from it.
Sets can also be used to perform mathematical set operations like
union, intersection, symmetric difference, etc.

my_set = {1, 2, 3}
print(my_set)
my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set)
Output
{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
PYTHON SETS
a = {}
print(type(a))
a = set()
print(type(a))
Output
PYTHON SETS
a = {}
print(type(a))
a = set()
print(type(a))
Output
<class 'dict'>
<class 'set'>

You might also like