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/python/python-dictionaries.md
+7-192Lines changed: 7 additions & 192 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,24 +7,20 @@ sidebar_position: 11
7
7
tags:
8
8
[
9
9
Python,
10
-
Introduction of python,
11
-
List in Python,
12
-
Python Syntax,
13
-
Variables,
14
-
Operators,
15
-
Type Casting,
16
-
String,
17
-
Tuple in Python,
18
-
Python Dictionaries
19
-
10
+
Python Dictionaries,
11
+
dict,
12
+
python data structures,
13
+
key-value pairs,
14
+
dictionary methods,
15
+
dictionary comprehension
20
16
]
21
17
22
18
---
23
19
24
20
25
21
# Python Dictionaries
26
22
27
-
A **dictionary** in Python is an unordered, mutable, and indexed collection of key-value pairs. It is one of the most powerful and flexible built-in data structures in Python, suitable for representing structured data.
23
+
A **dictionary** in Python is a **mutable** collection that stores data as **key-value pairs**. As of Python 3.7+, dictionaries are **ordered**, meaning they maintain the insertion order of items. It is one of the most powerful and flexible built-in data structures in Python, suitable for representing structured data.
28
24
29
25
## What is a Dictionary?
30
26
@@ -37,184 +33,3 @@ person = {
37
33
"age": 25,
38
34
"city": "New York"
39
35
}
40
-
````
41
-
42
-
## Properties of Dictionaries
43
-
44
-
* Keys are unique.
45
-
* Keys must be immutable.
46
-
* Values can be of any data type.
47
-
* Dictionaries are mutable and can be changed after creation.
48
-
* In Python 3.7+, dictionaries maintain insertion order.
49
-
50
-
## Creating Dictionaries
51
-
52
-
### Using Curly Braces:
53
-
54
-
```python
55
-
data = {"a": 1, "b": 2}
56
-
```
57
-
58
-
### Using the `dict()` Constructor:
59
-
60
-
```python
61
-
data =dict(x=10, y=20)
62
-
```
63
-
64
-
### Creating an Empty Dictionary:
65
-
66
-
```python
67
-
empty = {}
68
-
```
69
-
70
-
## Accessing Dictionary Elements
71
-
72
-
### Using Key Indexing:
73
-
74
-
```python
75
-
person["name"]
76
-
```
77
-
78
-
### Using `get()` Method:
79
-
80
-
```python
81
-
person.get("age")
82
-
person.get("gender", "Not Found")
83
-
```
84
-
85
-
## Adding and Updating Items
86
-
87
-
### Add New Key-Value:
88
-
89
-
```python
90
-
person["gender"] ="Female"
91
-
```
92
-
93
-
### Update Existing Key:
94
-
95
-
```python
96
-
person["age"] =30
97
-
```
98
-
99
-
### Use `update()` Method:
100
-
101
-
```python
102
-
person.update({"age": 35, "city": "Chicago"})
103
-
```
104
-
105
-
## Removing Elements
106
-
107
-
### Using `pop()`:
108
-
109
-
```python
110
-
person.pop("age")
111
-
```
112
-
113
-
### Using `del`:
114
-
115
-
```python
116
-
del person["city"]
117
-
```
118
-
119
-
### Using `clear()`:
120
-
121
-
```python
122
-
person.clear()
123
-
```
124
-
125
-
### Using `popitem()`:
126
-
127
-
Removes and returns the last inserted key-value pair.
0 commit comments