Python

Python List vs Tuples

Hello in this tutorial, we will see and understand the differences between List vs Tuples in Python programming.

1. Introduction

List and Tuple in python programming are used to store the data. List and Tuple data structures are similar in most cases but still have a few differences and we will understand them in this tutorial.

  • List in python are ordered, are homogeneous, can be accessed by index, are mutable and, are dynamic. The list is defined by enclosed within square brackets and elements are comma-separated.
    • Syntax representation – list_cars = ['dynamics', 'tynamics', 'alabaster', 'Bolt']
  • Tuple in python is defined by enclosing in oval parenthesis and elements are comma-separated. Tuple in python are heterogeneous and are immutable in nature. It has a fixed length (i.e. the size of the existing tuple cannot be changed). It is commonly used for unchangeable data.
    • Syntax representation – tuple_cars = ('dynamics', 'tynamics', 'alabaster', 10)

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python List vs Tuples

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 Python List vs Tuples

Let us understand this with the help of a code snippet.

Python List vs Tuples

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# python list vs tuples assignment
# python allocates memory to tuples in larger blocks with a low overhead as they are immutable
# tuples can contain elements belonging to different data-types
# tuples cannot be copied
# tuples have fixed length i.e. you cannot change the size of an existing tuple
tuple_cars = ('dynamics', 'tynamics', 'alabaster', 10)
print('tuple = {}'.format(tuple_cars))
print('size of tuple = {}'.format(tuple_cars.__sizeof__()))
print('\n')
# immutable tuples
# tuples do not support item assignment
# below print statement will throw an exception. uncomment it to see the effect
# tuple_cars[0] = 'Supremacy'
print('\n')
# python allocates memory to list in smaller blocks
# list can contain elements belonging to the same data-type
# list have variable length
list_cars = ['dynamics', 'tynamics', 'alabaster', 'Bolt']
print('list = {}'.format(list_cars))
print('size of list = {}'.format(list_cars.__sizeof__()))
print('\n')
# mutable list
list_cars[0] = 'mystery'
print('updated list = {}'.format(list_cars))

If everything goes well the following output will be shown in the IDE console.

Console Output

1
2
3
4
5
6
7
tuple = ('dynamics', 'tynamics', 'alabaster', 10)
size of tuple = 56
list = ['dynamics', 'tynamics', 'alabaster', 'Bolt']
size of list = 72
updated list = ['mystery', 'tynamics', 'alabaster', 'Bolt']

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • the differences between List vs Tuples data structure
  • Sample program to understand the List and Tuple implementation in python

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on List and Tuple in python programming.

Download
You can download the full source code of this example here: Python List vs Tuples

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button