dictionary Flashcards
d = {100:’durga’, 101:’ravi’, 102:’sagar’, 103:’chetan’}
d.setdefault(100,”Suhail”)
d
d.setdefault(200,”Ishita”)
d
d.setdefault(500)
d
> > > d.setdefault(100,”Suhail”)
‘durga’
d
{100:’durga’, 101:’ravi’, 102:’sagar’, 103:’chetan’}
d.setdefault(200,”Ishita”)
‘Ishita’
d
{100: ‘durga’, 101: ‘ravi’, 102: ‘sagar’, 103: ‘chetan’, 200: ‘Ishita’}
d.setdefault(500)
d
{100: ‘durga’, 101: ‘ravi’, 102: ‘sagar’, 103: ‘chetan’, 200: ‘Ishita’, 500: None}
Syntax of update()
dictionary.update(iterable)
iterable: A dictionary
Slicing is allowed in dictionary?
No
How to get items of dict using for loop
For k,v in d.items():
print(k,v)
If the specified value in pop() of dict and also default value is not given then
KeyError
Datatype of values of dictionary
Any data type
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = car.pop(“model”)
print(x)
Mustang
Return type of popitem()
tuple
How to get values of dict using for loop
For v in d.values():
print(v)
In dictionary, are duplicates allowed
For keys duplicates are not allowed
For values duplicates are allowed
How we can remove all entries from the dict but after deleting the elements still we can access dict
d.clear()
How do you create empty dictionary?
d = { }
Or
d = dict()
Take table
__________________
100 | ‘Krishna’
__________________
200 | ‘Ravi’
__________________
300 | ‘Shiva’
__________________
Write code for this table in python
d = {100: ‘Krishna’, 200: ‘Ravi’, 300: ‘Shiva’}
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(del d[100])
print(d)
print(del d[400])
print(d)
SyntaxError: invalid syntax
print(del d[100])
^^^
Dictionaries are mutable or immutable
Mutable
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = car.get(“hi”)
print(x)
None
get()
- Return the value associated with the key
- It doesn’t gives error
Dict
How to removes the item that was last inserted into the dictionary.
popitem() method
Syntax of d.pop()
dictionary.pop(keyname, defaultvalue)
keyname: Required. The keyname of the item you want to remove
defaultvalue: Optional. A value to return if the specified key do not exist.
If this parameter is not specified, and the no item with the specified key is found, an error is raised
Syntax of get()
dictionary.get(keyname, value)
keyname: Required. The keyname of the item you want to return the value from
value: Optional. A value to return if the specified key does not exist. Default value None
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = car.get(“price”, 15000)
print(x)
15000
How to get keys in python using for loop of dict
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
for k in d.keys():
print(k)
100
200
300
How to get each key value pair of dict
items()
data types of items of dictionary
Dictionary items contain 2 things
key values
Data type of Keys: Immutable: int, str, tuples, booleans, None,
frozenset
List, dictionaries, set can’t be used as keys as they are mutable
Data type of values: Any data type
len() in dict
Returns the number of items in dict
Indexing is allowed in dictionary?
No, even they preserved insertion order
Is dictionary changeable or not?
Changeable
How can we delete dict that we can’t even accessed it
del d
d[key1] = value1
If key1 is not present in dict, what will happen
new item will be added to dict i.e.key1:value1
How to returns the value of the item with the specified key.
If the key does not exist, it inserts the key with the specified value in dict
if the key does not exist and the specififed value is also not given then it will add that key with none in dict
In dict
setdefault()
Syntax of items()
dictionary.items()
No parameters
type of output of get()
str
Dictionary is ordered or not?
In python 3.6 or early, dictionary was unordered
now from 3.7, dictionary is ordered
d = {100:’Durga’, 200:’ravi’}
print(d[100])
print(d[400])
durga
KeyError: 400
When we use get(), If specified key is not present in dict and also default value is not given then
Get None
Dictionaries are dynamic or not
Dynamic i.e.
1. You can modify a dictionary after its creation by adding, updating, or deleting key-value pairs.
2. Dictionaries can grow and shrink dynamically in size as you modify them.
How can you access the data from dictionary
also example
By using keys
d = {100:’Durga’, 200:’Ravi’, 300:’Shiva’}
print([d[100]) #Durga
Syntax of setdefault()
dictionary.setdefault(keyname, value)
keyname Required. The keyname of the item you want to return the value from
value Optional.
If the key exist, this parameter has no effect.
If the key does not exist, this value becomes the key’s value
Default value None
Syntax of values()
dictionary.values()
No parameters
squares = {x: x*x for x in range(1,6)}
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
copy()
The copy() method returns a copy of the specified dictionary.
(Cloned copy)
How to delete elements from dictionary
By using
1.) del d[key]
2.) d.clear()
3.) del d
Type of output of values in dict and explain it
<class ‘dict_values’>
Its the view which display a list of the ditionary’s key-value tuples but the view is not the list, it csn be converted into one
What type of objects are allowed in dictionary?
for keys
For values
(heterogeneous /homogeneous)
heterogeneous for both keys and values
del d[key]
If key is not present in dict then?
we will get keyError
If the key does not exist in setdefault(), then
- If the key does not exist, this value becomes the key’s value.
- Default value None
How to get value associated with the key
dictionary.get(keyname, value)
Syntax of popitem()
dictionary.popitem()
No parameters
setdefault()
The setdefault() method returns the value of the item with the specified key.
If the key does not exist, insert the key, with the specified value
Default value None
In dict
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
del d[100]
print(d)
Del d[400]
print(d)
{200:’ravi’, 300:’shiva’}
KeyError: 400
How to return all values associated with the dictionary
Using values()
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
del d
print(d)
NameError
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.values())
dict_values([‘durga’, ‘ravi’, ‘shiva’])
How to remove the entry associated with specified key and return corresponding value in dict
Using pop()
How to deletes entry associated with the specified key in dict
del d[key]
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.pop(100))
print(d)
print(d.pop(400)
durga
{200:’ravi’, 300:’shiva’}
KeyError: 400
d = {}
d.popitem()
KeyError
d[key1] = value1
If key1 is already present in dict, what will happen
The old value will be replace by new value i.e. value1
values()
Returns all values associated with dictionary
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
x = car.get(“model”)
print(x)
Mustang
How do you create dictionary?
d= { }
OR
d = dict()
Syntax of copy()
dictionary.copy()
No parameters
How to inserts the specified dictionary to the dictionary.
dictionary.update(dictionary)
What type of brackets are used in to create dictionary?
Curly brackets { }
doubles = {x: 2*x for x in range(1,6)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
d.pop()
Removes the entry associated with the specified key and returns the corresponding value
How do we update dictionary?
d[key] = value
If the key is not available then a new item will be added to dict
If the key is already present the old value will be replace by new value
How can we create duplicate dictionary (cloned one)
copy()
clear()
To remove all entries from the dictionary
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
for v in d.values():
print(v)
durga
ravi
shiva
When we access dict by using keys and the specified key is not available then what will happen
You will get KeyError
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.popitem())
print(d)
(300, ‘shiva’)
{100: ‘durga’, 200: ‘ravi’}
Dictionary comprehension is applicable or not
Yes
type of dictionary
<class ‘dict’>
car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
car.pop(“model”)
print(car)
{‘brand’: ‘Ford’, ‘year’: 1964}
d={100:’durga’,200:’ravi’,300:’shiva’}
d.get(100)
d.get(400)
d.get(100,”Guest”)
d.get(400,”Guest”)
‘durga’
None
‘durga’
‘Guest’
Give example of dict comprehension
{1:1, 2:4, 3:9, 4:16, 5:25}
Squares = {x: x*x for x in range(1,6)}
print(squares)
update()
The update() method inserts the specified items to the dictionary.
The specified items can be a dictionary, or an iterable object with key value pairs.
del d
To delete total dictionary that we cannot accessed it after deletion
popitem()
The popitem() method removes the item that was last inserted into the dictionary. In versions before 3.7, the popitem() method removes a random item.
d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d)
d.clear()
print(d)
{100:’durga’, 200:’ravi’, 300:’shiva’}
{ }
return type of get()
‘str’
return type of d.pop()
‘str’
return type of popitem()
tuple
return type of keys()
<class ‘dict_keys’>
return type of values()
<class ‘dict_values’>
return type of items()
<class ‘dict_items’>
return type of setdefault()
‘str’
<class ‘dict_keys’> / <class ‘dict_values’>/ <class ‘dict_items’>
Returns a view which display a list of the dictionary’s (keys/values/key-value tuples) but the view is not a list, it can be converted into one