You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cheatsheet/dictionaries.md
+41-41
Original file line number
Diff line number
Diff line change
@@ -26,9 +26,9 @@ myCat = {
26
26
}
27
27
```
28
28
29
-
## The values() method
29
+
## values()
30
30
31
-
Get the **values** of the dictionary:
31
+
The `values()` method gets the **values** of the dictionary:
32
32
33
33
```python
34
34
>>> pet = {'color': 'red', 'age': 42}
@@ -39,9 +39,9 @@ Get the **values** of the dictionary:
39
39
# 42
40
40
```
41
41
42
-
## The keys() method
42
+
## keys()
43
43
44
-
Get the **keys** of the dictionary:
44
+
The `keys()` method gets the **keys** of the dictionary:
45
45
46
46
```python
47
47
>>> pet = {'color': 'red', 'age': 42}
@@ -52,9 +52,9 @@ Get the **keys** of the dictionary:
52
52
# age
53
53
```
54
54
55
-
## The items() method
55
+
## items()
56
56
57
-
Get the **items** of a dictionary and return them as a [Tuple](/cheatsheet/lists-and-tuples#the-tuple-data-type):
57
+
The `items()` method gets the **items** of a dictionary and return them as a <router-link to=/cheatsheet/lists-and-tuples#the-tuple-data-type>Tuple</router-link>:
58
58
59
59
```python
60
60
>>> pet = {'color': 'red', 'age': 42}
@@ -76,36 +76,9 @@ Using the `keys()`, `values()`, and `items()` methods, a for loop can iterate ov
76
76
# Key: age Value: 42
77
77
```
78
78
79
-
## Checking keys in a Dictionary
80
-
81
-
```python
82
-
>>> person = {'name': 'Rose', 'age': 33}
83
-
84
-
>>>'name'in person.keys()
85
-
# True
86
-
87
-
>>>'height'in person.keys()
88
-
# False
89
-
90
-
>>>'skin'in person # You can omit keys()
91
-
# False
92
-
```
93
-
94
-
## Checking values in a Dictionary
95
-
96
-
```python
97
-
>>> person = {'name': 'Rose', 'age': 33}
98
-
99
-
>>>'Rose'in person.values()
100
-
# True
101
-
102
-
>>>33in person.values()
103
-
# False
104
-
```
105
-
106
-
## The get() Method
79
+
## get()
107
80
108
-
The `get` method returns the value of an item with by using the key. If the key doesn't exist, it returns `None`:
81
+
The `get()` method returns the value of an item with by using the key. If the key doesn't exist, it returns `None`:
109
82
110
83
```python
111
84
>>> wife = {'name': 'Rose', 'age': 33}
@@ -129,7 +102,7 @@ You can also change the default `None` value for other of your choice:
129
102
# 'She is deeply in love with lover'
130
103
```
131
104
132
-
## The setdefault() Method
105
+
## Adding items with setdefault()
133
106
134
107
It's possible to add an item to a dictionary in this way:
135
108
@@ -139,7 +112,7 @@ It's possible to add an item to a dictionary in this way:
139
112
... wife['has_hair'] =True
140
113
```
141
114
142
-
Using `setdefault` we could make the same code more short:
115
+
Using the `setdefault`method we can make the same code more short:
143
116
144
117
```python
145
118
>>> wife = {'name': 'Rose', 'age': 33}
@@ -152,7 +125,7 @@ Using `setdefault` we could make the same code more short:
152
125
153
126
### pop()
154
127
155
-
`pop()` removes an item based on a given key.
128
+
The `pop()` method removes an item based on a given key.
0 commit comments