DEV Community

importox
importox

Posted on

1

Tuples in Python

Python Tuples basic concepts are introduced in this aritcle.
Tuples in Python with some examples. A tuple is one of Python's data types

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 list in Python with the keyword list(). A Python list can be cast into tuples 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 Python shell and from a Python script

Creating Tuples

    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:

    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:

    tup1  = 'Python','Tuple','Lists'
    tup2 = ('Python','Tuple','Lists','Devto','best')

    print('Concatenation of Tuples:\n',tup1+tup2)

The program above outputs this:

    Concat of tuples: 
    ('Python', 'Tuple', 'Lists', 'Python', 'Tuple', 'Lists', 'Devto', 'best')

Convert list into tuples and tuples into list

    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,

    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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (1)

Collapse
 
waylonwalker profile image
Waylon Walker

I use lists everywhere just because lists seem to be the standard. The immutable nature of tuples should make them superior in any case where you do not indent to modify it. But it doesn't seem to be the case.

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay