dictionary Flashcards

(90 cards)

1
Q

d = {100:’durga’, 101:’ravi’, 102:’sagar’, 103:’chetan’}
d.setdefault(100,”Suhail”)
d
d.setdefault(200,”Ishita”)
d
d.setdefault(500)
d

A

> > > 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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Syntax of update()

A

dictionary.update(iterable)

iterable: A dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Slicing is allowed in dictionary?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to get items of dict using for loop

A

For k,v in d.items():
print(k,v)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If the specified value in pop() of dict and also default value is not given then

A

KeyError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Datatype of values of dictionary

A

Any data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.pop(“model”)

print(x)

A

Mustang

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Return type of popitem()

A

tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to get values of dict using for loop

A

For v in d.values():
print(v)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In dictionary, are duplicates allowed

A

For keys duplicates are not allowed
For values duplicates are allowed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How we can remove all entries from the dict but after deleting the elements still we can access dict

A

d.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create empty dictionary?

A

d = { }
Or
d = dict()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Take table
__________________
100 | ‘Krishna’
__________________
200 | ‘Ravi’
__________________
300 | ‘Shiva’
__________________

Write code for this table in python

A

d = {100: ‘Krishna’, 200: ‘Ravi’, 300: ‘Shiva’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(del d[100])
print(d)
print(del d[400])
print(d)

A

SyntaxError: invalid syntax
print(del d[100])
^^^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Dictionaries are mutable or immutable

A

Mutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.get(“hi”)

print(x)

A

None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

get()

A
  • Return the value associated with the key
  • It doesn’t gives error

Dict

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to removes the item that was last inserted into the dictionary.

A

popitem() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Syntax of d.pop()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Syntax of get()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

car = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}

x = car.get(“price”, 15000)

print(x)

A

15000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to get keys in python using for loop of dict

A

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
for k in d.keys():
print(k)

100
200
300

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to get each key value pair of dict

A

items()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

data types of items of dictionary

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
len() in dict
Returns the number of items in dict
26
Indexing is allowed in dictionary?
No, even they preserved insertion order
27
Is dictionary changeable or not?
Changeable
28
How can we delete dict that we can’t even accessed it
del d
29
d[key1] = value1 If key1 is not present in dict, what will happen
new item will be added to dict i.e.key1:value1
30
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()
31
Syntax of items()
dictionary.items() No parameters
32
type of output of get()
str
33
Dictionary is ordered or not?
In python 3.6 or early, dictionary was unordered now from 3.7, dictionary is ordered
34
d = {100:’Durga’, 200:’ravi’} print(d[100]) print(d[400])
durga KeyError: 400
35
When we use get(), If specified key is not present in dict and also default value is not given then
Get None
36
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.
37
How can you access the data from dictionary also example
By using keys d = {100:’Durga’, 200:’Ravi’, 300:’Shiva’} print([d[100]) #Durga
38
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
39
Syntax of values()
dictionary.values() No parameters
40
squares = {x: x*x for x in range(1,6)}
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
41
copy()
The copy() method returns a copy of the specified dictionary. (Cloned copy)
42
How to delete elements from dictionary
By using 1.) del d[key] 2.) d.clear() 3.) del d
43
Type of output of values in dict and explain it
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
44
What type of objects are allowed in dictionary? for keys For values (heterogeneous /homogeneous)
heterogeneous for both keys and values
45
del d[key] If key is not present in dict then?
we will get keyError
46
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
47
How to get value associated with the key
dictionary.get(keyname, value)
48
Syntax of popitem()
dictionary.popitem() No parameters
49
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
50
d = {100:'durga', 200:'ravi', 300:'shiva'} del d[100] print(d) Del d[400] print(d)
{200:’ravi’, 300:’shiva’} KeyError: 400
51
How to return all values associated with the dictionary
Using values()
52
d = {100:'durga', 200:'ravi', 300:'shiva'} del d print(d)
NameError
53
d = {100:'durga', 200:'ravi', 300:'shiva'} print(d.values())
dict_values(['durga', 'ravi', 'shiva'])
54
How to remove the entry associated with specified key and return corresponding value in dict
Using pop()
55
How to deletes entry associated with the specified key in dict
del d[key]
56
d = {100:'durga', 200:'ravi', 300:'shiva'} print(d.pop(100)) print(d) print(d.pop(400)
durga {200:'ravi', 300:'shiva'} KeyError: 400
57
d = {} d.popitem()
KeyError
58
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
59
values()
Returns all values associated with dictionary
60
car = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = car.get("model") print(x)
Mustang
61
How do you create dictionary?
d= { } OR d = dict()
62
Syntax of copy()
dictionary.copy() No parameters
63
How to inserts the specified dictionary to the dictionary.
dictionary.update(dictionary)
64
What type of brackets are used in to create dictionary?
Curly brackets { }
65
doubles = {x: 2*x for x in range(1,6)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
66
d.pop()
Removes the entry associated with the specified key and returns the corresponding value
67
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
68
How can we create duplicate dictionary (cloned one)
copy()
69
clear()
To remove all entries from the dictionary
70
d = {100:'durga', 200:'ravi', 300:'shiva'} for v in d.values(): print(v)
durga ravi shiva
71
When we access dict by using keys and the specified key is not available then what will happen
You will get KeyError
72
d = {100:'durga', 200:'ravi', 300:'shiva'} print(d.popitem()) print(d)
(300, 'shiva') {100: 'durga', 200: 'ravi'}
73
Dictionary comprehension is applicable or not
Yes
74
type of dictionary
75
car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car.pop("model") print(car)
{'brand': 'Ford', 'year': 1964}
76
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'
77
Give example of dict comprehension
Squares = {x: x*x for x in range(1,6)} print(squares) #{1:1, 2:4, 3:9, 4:16, 5:25}
78
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.
79
del d
To delete total dictionary that we cannot accessed it after deletion
80
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.
81
d = {100:'durga', 200:'ravi', 300:'shiva'} print(d) d.clear() print(d)
{100:'durga', 200:'ravi', 300:'shiva'} { }
82
return type of get()
'str'
83
return type of d.pop()
'str'
84
return type of popitem()
tuple
85
return type of keys()
86
return type of values()
87
return type of items()
88
return type of setdefault()
'str'
89
/ /
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
90