> ## 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 List Examples

### Create a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1)
print(type(list1))
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
<class 'list'>
```

### Create an empty list

```python theme={"system"}
list1 = []
print(list1)
print(type(list1))
```

```
Output:
[]
<class 'list'>
```

### Access an element in a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[0])
```

```
Output:
1
```

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

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[-1])
```

```
Output:
10
```

### Access a range of elements in a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1[0:5])
```

```
Output:
[1, 2, 3, 4, 5]
```

### Add an element to a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.append(11)
print(list1)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
```

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

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.insert(0, 0)
print(list1)
```

```
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

### Remove an element from a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.remove(10)
print(list1)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```

### Remove an element from a list

* at a specific index

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.pop(0)
print(list1)
```

```
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10]
```

* using del

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del list1[0]
print(list1)
```

```
Output:
[2, 3, 4, 5, 6, 7, 8, 9, 10]
```

### Remove all elements from a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.clear()
print(list1)
```

```
Output:
[]
```

### Check if an element is in a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(11 in list1)
print(1 in list1)
```

```
Output:
False
True
```

### Get the length of a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(len(list1))
```

```
Output:
10
```

### Sort a list

```python theme={"system"}
list1 = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
list1.sort()
print(list1)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

### Sort a list in reverse order

```python theme={"system"}
list1 = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
list1.sort(reverse=True)
print(list1)
```

```
Output:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
```

### Reverse a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1.reverse()
print(list1)
```

```
Output:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
```

### Copy a list

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = list1.copy()
print(list2)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

### Join two lists

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
list3 = list1 + list2
print(list3)
print(type(list3))
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
<class 'list'>
```

* using extend

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
list1.extend(list2)
print(list1)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
```

### Remove duplicates from a list

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

```python theme={"system"}
list1 = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10]
list1 = list(set(list1))
print(list1)
```

```
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

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

```python theme={"system"}
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1.index(5))
```

```
Output:
4
```
