dictionary Flashcards

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
Q

len() in dict

A

Returns the number of items in dict

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

Indexing is allowed in dictionary?

A

No, even they preserved insertion order

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

Is dictionary changeable or not?

A

Changeable

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

How can we delete dict that we can’t even accessed it

A

del d

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

d[key1] = value1
If key1 is not present in dict, what will happen

A

new item will be added to dict i.e.key1:value1

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

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

A

setdefault()

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

Syntax of items()

A

dictionary.items()

No parameters

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

type of output of get()

A

str

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

Dictionary is ordered or not?

A

In python 3.6 or early, dictionary was unordered
now from 3.7, dictionary is ordered

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

d = {100:’Durga’, 200:’ravi’}
print(d[100])
print(d[400])

A

durga
KeyError: 400

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

When we use get(), If specified key is not present in dict and also default value is not given then

A

Get None

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

Dictionaries are dynamic or not

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How can you access the data from dictionary
also example

A

By using keys
d = {100:’Durga’, 200:’Ravi’, 300:’Shiva’}
print([d[100]) #Durga

38
Q

Syntax of setdefault()

A

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
Q

Syntax of values()

A

dictionary.values()

No parameters

40
Q

squares = {x: x*x for x in range(1,6)}

A

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

41
Q

copy()

A

The copy() method returns a copy of the specified dictionary.
(Cloned copy)

42
Q

How to delete elements from dictionary

A

By using

1.) del d[key]
2.) d.clear()
3.) del d

43
Q

Type of output of values in dict and explain it

A

<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

44
Q

What type of objects are allowed in dictionary?

for keys
For values
(heterogeneous /homogeneous)

A

heterogeneous for both keys and values

45
Q

del d[key]
If key is not present in dict then?

A

we will get keyError

46
Q

If the key does not exist in setdefault(), then

A
  • If the key does not exist, this value becomes the key’s value.
  • Default value None
47
Q

How to get value associated with the key

A

dictionary.get(keyname, value)

48
Q

Syntax of popitem()

A

dictionary.popitem()

No parameters

49
Q

setdefault()

A

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
Q

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

A

{200:’ravi’, 300:’shiva’}
KeyError: 400

51
Q

How to return all values associated with the dictionary

A

Using values()

52
Q

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

A

NameError

53
Q

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.values())

A

dict_values([‘durga’, ‘ravi’, ‘shiva’])

54
Q

How to remove the entry associated with specified key and return corresponding value in dict

A

Using pop()

55
Q

How to deletes entry associated with the specified key in dict

A

del d[key]

56
Q

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.pop(100))
print(d)
print(d.pop(400)

A

durga
{200:’ravi’, 300:’shiva’}
KeyError: 400

57
Q

d = {}
d.popitem()

A

KeyError

58
Q

d[key1] = value1
If key1 is already present in dict, what will happen

A

The old value will be replace by new value i.e. value1

59
Q

values()

A

Returns all values associated with dictionary

60
Q

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

x = car.get(“model”)

print(x)

A

Mustang

61
Q

How do you create dictionary?

A

d= { }
OR
d = dict()

62
Q

Syntax of copy()

A

dictionary.copy()

No parameters

63
Q

How to inserts the specified dictionary to the dictionary.

A

dictionary.update(dictionary)

64
Q

What type of brackets are used in to create dictionary?

A

Curly brackets { }

65
Q

doubles = {x: 2*x for x in range(1,6)}

A

{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}

66
Q

d.pop()

A

Removes the entry associated with the specified key and returns the corresponding value

67
Q

How do we update dictionary?

A

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
Q

How can we create duplicate dictionary (cloned one)

A

copy()

69
Q

clear()

A

To remove all entries from the dictionary

70
Q

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

A

durga
ravi
shiva

71
Q

When we access dict by using keys and the specified key is not available then what will happen

A

You will get KeyError

72
Q

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d.popitem())
print(d)

A

(300, ‘shiva’)
{100: ‘durga’, 200: ‘ravi’}

73
Q

Dictionary comprehension is applicable or not

A

Yes

74
Q

type of dictionary

A

<class ‘dict’>

75
Q

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

car.pop(“model”)

print(car)

A

{‘brand’: ‘Ford’, ‘year’: 1964}

76
Q

d={100:’durga’,200:’ravi’,300:’shiva’}
d.get(100)
d.get(400)
d.get(100,”Guest”)
d.get(400,”Guest”)

A

‘durga’
None
‘durga’
‘Guest’

77
Q

Give example of dict comprehension

A

{1:1, 2:4, 3:9, 4:16, 5:25}

Squares = {x: x*x for x in range(1,6)}
print(squares)

78
Q

update()

A

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
Q

del d

A

To delete total dictionary that we cannot accessed it after deletion

80
Q

popitem()

A

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
Q

d = {100:’durga’, 200:’ravi’, 300:’shiva’}
print(d)
d.clear()
print(d)

A

{100:’durga’, 200:’ravi’, 300:’shiva’}
{ }

82
Q

return type of get()

A

‘str’

83
Q

return type of d.pop()

A

‘str’

84
Q

return type of popitem()

A

tuple

85
Q

return type of keys()

A

<class ‘dict_keys’>

86
Q

return type of values()

A

<class ‘dict_values’>

87
Q

return type of items()

A

<class ‘dict_items’>

88
Q

return type of setdefault()

A

‘str’

89
Q

<class ‘dict_keys’> / <class ‘dict_values’>/ <class ‘dict_items’>

A

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
Q
A