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

> Python Tuple Examples with code

### Create a tuple

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

### Create an empty tuple

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

```
Output:
()
<class 'tuple'>
```

### Access an element in a tuple

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

```
Output:
1
```

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

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

```
Output:
10
```

### Access a range of elements in a tuple

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

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

### Add an element to a tuple

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

```
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
```

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

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

```
Output:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
```

### Remove an element from a tuple

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

```
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9)
```

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

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

```
Output:
(2, 3, 4, 5, 6, 7, 8, 9, 10)
```

### Remove an element from a list using del

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

```
Output:
(2, 3, 4, 5, 6, 7, 8, 9, 10)
```

### Remove all elements from a list

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

```
Output:
()
```

### Check if an element is in a list

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

```
Output:
False
True
```

### Get the length of a list

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

```
Output:
10
```

### Sort a list

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

```
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
```

### Sort a list in reverse order

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

```
Output:
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
```

### Reverse a list

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

```
Output:
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
```

### Copy a list

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

```
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
```

### Join two lists

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

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

### Join two lists using extend

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

```
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"}
tuple1 = (1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10)
tuple1 = tuple(set(tuple1))
print(tuple1)
```

```
Output:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
```

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

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

```
Output:
4
```
