VOOZH about

URL: https://thenewstack.io/python-for-beginners-when-and-how-to-use-tuples/

⇱ Python for Beginners: When and How to Use Tuples - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-03-30 06:00:11
Python for Beginners: When and How to Use Tuples
tutorial,
Data / Software Development

Python for Beginners: When and How to Use Tuples

Let's learn what a Python tuple is and how to write/use them.
Mar 30th, 2022 6:00am by Jack Wallen
👁 Featued image for: Python for Beginners: When and How to Use Tuples

Python is a very powerful and flexible programming language. It also happens to be one of the best languages for beginners, because it’s not only so easy to learn, it doesn’t require complicated compilers, and, instead, uses an interpreter to run programs. So instead of writing a program, compiling it, and running it…you simply write it and run it.

For the most part, Python is quite simple to grasp. We’ve already covered reading text from files, the Range function, data types, and numerous other concepts surrounding this user-friendly language.

But not every concept is as easy to grasp. One such idea is the tuple. A tuple is a single variable that can hold multiple items. Although once you understand what this feature does, it’s rather simple to work with. Like Python sets, lists and dictionaries, tuples are built-in data types, but faster to work with than those other choices.

With that said, let’s learn what a tuple is and how to write/use them.

What Is a Tuple?

In simplest terms, a Python tuple is similar to a list, which is a simple way of defining multiple variables.

So instead of defining your variables like so:

color1 = "blue"
color2 = "green"
color3 = "red"
color4 = "yellow"

You could then print out that list of variables like so:

print (color1,color2,color3,color4)

That’s all fine and good, but with a list is much easier. To get the same effect, a list of colors would look like this:

color = ['blue','green','red','yellow']

You could then print out the list like so:

print (color[0], color[1], color[2], color[3])

Using lists makes it much easier to efficiently define similar variables.

Similar to lists, a tuple is a collection of values that are declared using parentheses. So instead of this list:

names = ['Olivia', 'Nathan', 'Bethany', 'Jacob']

We format tuples like this:

names = ('Olivia', 'Nathan', 'Bethany', 'Jacob')

Beyond the obvious usage of [] vs (), what’s the difference between a tuple and a list? Simply put, you can change the elements of a list (once they are assigned), whereas with a tuple you cannot.

Want to see the difference from the perspective of the Python interpreter? Open a terminal window on a machine that has Python installed and access the console with the command:

python3

Type:

names_l = ['Olivia', 'Nathan', 'Bethany', 'Jacob']

Now, type:

names_t = ('Olivia', 'Nathan', 'Bethany', 'Jacob')

Okay, now type:

print(type(names_l))

Hit Enter on your keyboard and you should see:

<class 'list'>

Type:

print(type(names_t))

Hit Enter on your keyboard and you should see:

<class 'tuple'>

So far, the difference is minimal to the programmer. One of the keys to using tuples over lists is that lists cannot be used in dictionaries (because lists are mutable).

Hold up a minute. What is this dictionary you speak of? A Python dictionary is similar to a list, in that it is a collection of data. Dictionaries are Python’s implementation of associate arrays and use key-value pairs like so:

NBA_TEAM = {

... 'Indiana' : 'Pacers',

... 'Boston' : 'Celtics',

... 'Los Angeles' : 'Lakers',

... 'Milwaukee' : 'Bucks'

... }

You can then print out the dictionary like so:

print (NBA_TEAM) 

Types of Tuples

There are four different types of tuples you can use. First, there’s the empty tuple, which is written like this:

empty_tuple = ()

Print out that tuple (with the command print(empty_tuple)) and you’ll see nothing but () as the output.

The next type of tuple is one with integers, which looks like this:

integer_tuple = (1, 2, 3)

Print out that tuple and you should see:

(1, 2, 3)

Next, we have a tuple with mixed data types, which might look like this:

mixed_tuple = (0, "Hello", 1.2, "World!")

Print out that tuple and you should see something like:

(0, 'Hello', 1.2, 'World!')

Finally, we have the nested tuple which is a tuple within a tuple and looks like this:

nested_tuple = ("aardvark", [0, 1, 2], (2, 1, 0))

As you might expect, the output of print(nested_tuple) will look something like:

('aardvark', [0, 1, 2], (2, 1, 0))

Accessing Tuple Elements

This is where tuples can get a bit tricky. Let’s create a basic tuple like so:

tns_tuple = ('N', 'e', 'w', 'S', 't', 'a', 'c', 'k')

Now, issue the command:

print(tns_tuple[0])

What you should see printed out is:

N

Remember, in Python, counting starts at 0, so the N is at the 0 position in the tuple.

Where it gets a bit tricky is when you want to index a nested tuple. Let’s create the following nested tuple:

n_tuple = ("kubernetes", "cloud native", [8, 6, 7, 5, 3, 0, 9])

Let’s access various elements from within that nested tuple. Because this is nested, we’ve effectively created three tuples:

tuple 0 = kubernetes
tuple 1 = cloud native
tuple 3 = 8, 6, 7, 5, 3, 0, 9

If I want to print out the element 3 in tuple 0 (which would be the letter “e” in “kubernetes”), I would issue the command:

print(n_tuple[0][3])

What I’ve done is tell the Python interpreter to print out the value for the third position in the first tuple (tuple 0).

We can also access a range of our items within a tuple by doing what’s called slicing. This is done by using the : character. Let’s go back to our basic tuple:

tns_tuple = ('N', 'e', 'w', 'S', 't', 'a', 'c', 'k')

To print out the first three elements, we could do something like:

print(tns_tuple[0:3])

That command would print out:

('N', 'e', 'w')

To print out the last five elements, the command would be:

print(tns_tuple[3:8])

The above command would print out:

('S', 't', 'a', 'c', 'k')

We can use concatenation and repetition with tuples. Concatenation is done with the + operator like so:

print(('T', 'h', 'e') + ('N', 'e', 'w') + ('S', 't', 'a', 'c', 'k') )

The output of that concatenated tuple looks like this:

('T', 'h', 'e', 'N', 'e', 'w', 'S', 't', 'a', 'c', 'k')

We can use repetition with the * operator like so:

print(("The New Stack",) * 3)

The output of the above command would look like this:

('The New Stack', 'The New Stack', 'The New Stack')

Finally, another really handy feature with tuples is the ability to count and index items. Let’s go back to our basic tuple (adding a bit to it):

tns_tuple = ('N', 'e', 'w', 'S', 't', 'a', 'c', 'k', 'R', 'o', 'k', 's')

Let’s say you want to count how many k’s are in the tuple. This can be done like so:

print(tns_tuple.count('k'))

The output of the above command would be 2 because there are two k’s in the tuple. But what positions to those k’s hole? Let’s find out with:

print(tns_tuple.index('k'))

Ah, we’ve run into a problem. The above command will print out 7 and stop. It won’t continue indexing. That’s because tuple.index was designed to do just that.

In order to locate the second instance, we have to use a bit of trickery like so:

i = tns_tuple.index('k')
tns_tuple.index('k', i + 1)

That should print out:

10

And there you have it…your introduction to Python tuples. Make sure to go back through the rest of the series and keep coming here to The New Stack for more Python goodness.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Simply.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.