Tuples in Python
Tags: python
Published: 2020-06-09 11:33:04

Python Tuples basic concepts are introduced in this aritcle.
Tuples in Python with some examples.  A tuple is one of Python's <a href="https://python-commandments.org/python-data-types/">data types</a>

## Python Tuples

*Python tuples* are a group of elements separated by commas in a round bracket.

**Tuples** are like list, they are a sequence. You can get the index, get a slice etc.

But lists are mutable whereas tuples are immutable.

A Python tuple can be cast into <a href="https://pythonprogramminglanguage.com/lists/">list</a> in Python with the keyword `list()`.  A Python list can be cast into <a href="https://pythonspot.com/python-tuples/">tuples</a> by using keyword **"tuple"**.

Here you will see some examples to understand **how to create tuples?** ,**convert lists into tuples?** , **concatenation of tuples** , **nestedtuples** , etc.

This works in both the <a href="https://bsdnerds.org/learn-python/">Python shell</a> and from a <a href="https://pythonbasics.org/execute-python-scripts/">Python script</a>

### Creating Tuples    
    
```python
    tup='Python','Tuple','Lists'
    tup2=('Python','Tuple','Lists','Devto','best')
    
    e_tup=()
    
    u_l_tup=('devto',)
    
    print('empty tuple:',e_tup)
    print('Unit length tuple:',u_l_tup)
    print('first tuple:',tup)
    print('Second tuple:',tup2)
```    

**Note:** When you are going to create a tuple of length 1 in Python then you should put a comma after the element before closing the round brackets.

The program above outputs this:    
   
```python 
    empty tuple: ()
    Unit length tuple: ('devto',)
    first tuple: ('Python', 'Tuple', 'Lists')
    Second tuple: ('Python', 'Tuple', 'Lists', 'Devto', 'best')
```    

Here, you have seen that if you are creating tuples with round brackets or not both results are the same.

### Concatenation of Tuples

Add tuples like lists by using plus (+) sign between it. Let's suppose the same tuples that you have created just above and try to add these tuples.

**Program:**    
   
```python 
    tup1  = 'Python','Tuple','Lists'
    tup2 = ('Python','Tuple','Lists','Devto','best')
    
    print('Concatenation of Tuples:\n',tup1+tup2)
```    

The program above outputs this:    
   
```python 
    Concat of tuples: 
    ('Python', 'Tuple', 'Lists', 'Python', 'Tuple', 'Lists', 'Devto', 'best')
```    

### Convert list into tuples and tuples into list    
   
```python 
    tup=('Python','Tuple','Lists','Devto','best')
    
    l=list(tup) #now l is list.
    tup2=tuple(l) #Here list is converted into tuple.
    
    print('tuple is converted into list:',tup)
    print('list is converted into tuple:',tup2)
```

The program above outputs this:    
    
    tuple is converted into list: ['Python', 'Tuple', 'Lists', 'Devto', 'best']
    list is converted into tuple: ('Python','Tuple','Lists','Devto','best')
    

### Nested tuples

**Nested tuples** means one tuple inside another (this is like nested lists). 

To **create a nested tuple** , you have to put both or more tuples in the round brackets separated with commas. 

Let's look at this Python program,    
   
```python 
    tup='Python','Tuple','Lists'
    tup2=('Python','Tuple','Lists','Devto','best')
    
    nested_tup=(tup,tup2)
    
    print('Nested tuple:',nested_tup)
```    

The program above outputs this:    
    
    Nested tuple: (('Python', 'Tuple', 'Lists'), ('Python', 'Tuple', 'Lists', 'Devto', 'best'))
    
**Related links**
* <a href="https://gumroad.com/l/dcsp">Python Crash Course and book</a>
* <a href="https://docs.python.org/3.8/library/functions.html">Python documentation on tuples</a>