> ## Documentation Index
> Fetch the complete documentation index at: https://rajanand.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Dictionary Examples

> Python Dictionary Examples with code

### Create a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(dict1)
print(type(dict1))
```

### Create an empty dictionary

```python theme={"system"}
dict1 = {}
print(dict1)
print(type(dict1))
```

```
Output:
{}
<class 'dict'>
```

### Access an element in a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(dict1[1])
```

```
Output:
one
```

### Access an element in a dictionary using negative index

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(dict1[-1])
```

```
Output:
five
```

### Access a range of elements in a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(dict1[0:5])
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Add an element to a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1[6] = "six"
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
```

### Add an element to a dictionary at a specific index

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1[6] = "six"
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
```

### Remove an element from a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.pop(6)
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Remove an element from a dictionary at a specific index

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.pop(0)
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Remove an element from a dictionary using del

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
del dict1[0]
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Remove all elements from a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.clear()
print(dict1)
```

```
Output:
{}
```

### Check if an element is in a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(11 in dict1)
print(1 in dict1)
```

```
Output:
False
True
```

### Get the length of a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
print(len(dict1))
```

```
Output:
5
```

### Sort a list

```python theme={"system"}
dict1 = {6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.sort()
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}
```

### Sort a list in reverse order

```python theme={"system"}
dict1 = {6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.sort(reverse=True)
print(dict1)
```

```
Output:
{10: 'ten', 9: 'nine', 8: 'eight', 7: 'seven', 6: 'six', 5: 'five', 4: 'four', 3: 'three', 2: 'two', 1: 'one'}
```

### Reverse a list

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict1.reverse()
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Copy a list

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict2 = dict1.copy()
print(dict2)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
```

### Join two lists

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict2 = {6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten"}
dict3 = dict1 + dict2
print(dict3)
print(type(dict3))
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}
<class 'dict'>
```

### Join two dictionaries using update

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"}
dict2 = {6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten"}
dict1.update(dict2)
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}
```

### Remove duplicates from a dictionary

Set is a collection of unique elements. set is used to remove duplicates from a dictionary.

```python theme={"system"}
dict1 = {1: "one", 2: "two", 2: "two", 3: "three", 4: "four", 4: "four", 5: "five", 6: "six", 6: "six", 7: "seven", 8: "eight", 8: "eight", 9: "nine", 10: "ten", 10: "ten" }
dict1 = set(dict1)
print(dict1)
```

```
Output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}
```

### Find the index of an element in a dictionary

```python theme={"system"}
dict1 = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten"}
print(dict1.index(5))
```

```
Output:
4
```
