Untitled
Untitled
Untitled
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.
{'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}
# value is provided
print('Salary: ', person.get('salary', 0.0))
person = {'name': 'Phill', 'age': 22}
# 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.
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.
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.
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.
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'>