List and Tuple | Detailed Discussion | Python | 2022

In this article, we’ll look at List and Tuple and how they’re different. What actions may be performed on a list and a tuple independently, as well as what functions can be used in both?

List and Tuple | Detailed Discussion | Python | 2022 1
Article Image

Before we get into the differences, let’s take a look at lists and tuples.

What is a List in Python

A list is an ordered collection of elements of various data types, or simply a sequence of data.

One of Python’s 4 built-data structures for storing data and managing data activities is the list. Due to the fact that it is typically used to hold the same type of data, it is somewhat comparable to Array in other languages like C or C++.

However, it differs in that it can include several data types and is dynamic, meaning that its size can fluctuate.

How to Declare a List

  • Declare any variable, then every data in a list must be enclosed in square brackets [ ].
  • Initialized data in square brackets should be declared in single, double, or triple inverted commas, separated by commas.
# Declaration of list                
list = ['Beetechnical', 'is','1','technical','site']

# output of list
print (list)
How to declare a list in python
Output of list

What is Tupple in Python

One of Python’s four built-in data structures for storing data is called a “tuple.” It is fairly akin to the typical C and C++  structures used to store various data types that are the same throughout the application.

It differs from a structure, as a structure has user-defined functionality, whereas a tuple has built-in functionality and values can’t be changed after it has been initialized in starting.

Declaration of Tuple

  • Declare any variable, then every data in a tuple enclosed in parenthesis ( ) or without parenthesis.
  • Initialized data should be declared in single, double, or triple inverted separated by commas.
# Declaration of tuple 
tuple = 'Beetechnical', 'is','1','technical','site'

# output of tuple
print (tuple)
How to declare a tuple in python
Output for Tuple

Comparison Between Lists and Tuple in Python

DomainListTuple
DeclarationSquare brackets are mandatory. [ ]Parentheses aren’t mandatory. ( )
Time complexity It has more time complexity.It has less time complexity.
Space complexityIt has more space complexity.It has less space complexity.
Complexity reasonIt has allocated 2 blocks of memory.
One is fixed for all Python data and object variables.
Another is dynamic for operations on data.
It has only 1 block of memory. It doesn’t require extra memory to store new data.
MutabilityIt is mutable i.e. one type of data can be converted into another type.It is immutable i.e. data can’t be changed once assigned.
Function and operationsIt has more functions and operations as it can be used as a stack and queue.It has relatively low function and operations.
ErrorsIt gives many unexpected errors.It has relatively low unexpected errors.
PackingIt only supports packing and sometimes unpacking.It supports both packing and unpacking.
Difference

Differences in declarations are already illustrated in the item above, on to our following difference.

Calculate Time Taken by List and Tuple

In Python, we utilize the time function to calculate the time.

  • Our program just imports time, which will display how long it takes our code to execute.
  • The list and tuple codes are provided below so that you can see how long each of them takes to execute.
  • This will allow you to compare the time complexity of the two functions.

Code :

import time

# function for time
Program_time = time.time()

print(Program_time)

Output :

List and Tuple | Detailed Discussion | Python | 2022 2
Output for time

List | Time :

  • We use the time() function to determine the time in List.
  • We import time, along with sys, platform, and occasionally random, into our program to see how long our List code runs.
import sys, platform
import time
 
l=list(range(10000001))   
 
start = time.time()           # start of time
for i in range(len(l)):
    a = t[i]
end = time.time()             # end of time
print("Time taken by List: ", end - start)
Time taken by list in python
Time taken by list
  • Here we are time. time() starts by recording the overall time required to execute the function, including importing additional files.
  • end = time. time() keeps track of time up to the point at which it is applied and includes looping and importing times.
  • Therefore, the end-start offers us precise timing for looping. Additionally, it varies from compiler to compiler.

Tuple | Time :

We use time() to determine the time in a tuple. We import time, along with sys, platform, and sometimes random, into our program, and it tells us how long it takes to execute tuple code.

import sys, platform
import time
 
t=tuple(range(10000001))
 
start = time.time()
for i in range(len(t)):
    a = t[i]
end = time.time()
print("Time taken by Tuple: ", end - start)
Tuple time
Tuple time

We can see from the above program that tuples take less time than lists. Because it was a demonstration program, the difference does not appear to be very large; however, for a large program, the difference is significant.

Space Allocation When Using List Vs Tuple

In this part, we’ll compare the amount of space a list and a tuple take up for the same length of data type, as well as how complex those spaces are. We already know that lists require a lot of space because they can be modified and require two memory blocks.


The code for space complexity is shown below. For space, we use the function sys.getsizeof(element).

Code :

import sys

tuple = 'Beetechnical', 'is','1','technical','site'

list = ['Beetechnical', 'is','1','technical','site']

print(sys.getsizeof(tuple))
print(sys.getsizeof(list))
List and Tuple | Detailed Discussion | Python | 2022 3
Space of list and Tuple

It is clear that a list uses approximately a third more space than a tuple. So we can use a tuple where we need a constant array.

Mutability

It’s something we can alter when we’re mutable. It implies that we are able to turn one type of object into another. Only the tuple built-in function in Python’s set of four built-in data storage functions is immutable; the others are lists, sets, and dictionaries.
The code below displays mutuality.

Code (List) :

list = ['Beetechnical', 'is','1','technical','site']
  
list[2]='a'      # to chnage element at pos 2

print(list)

list.append('Great')     # to add element at last

print(list)

Output :

Mutability of list object in Python
Output for List

Code (Tuple):

tuple = 'Beetechnical', 'is','1','technical','site'
  
tuple[2]='a'      # to chnage element at pos 2

print(tuple)

tuple.append('Great')     # to add element at last

print(tuple)

Output :

The mutability of tuple object in Python
Output
  • .append() here adds elements to the list’s end. While tuple does not favor this attribute.
  • Tuple isn’t immutable, and it doesn’t permit list assignment or adding additional elements, such as appending, as can be seen from the two lines of code above.

Function and Operation

The different functions, such as insert(), clear(), sort(), pop(), reverse(), remove(), and append(), can only be used in lists, not in tuples ().

Code (List) :

# list supported functios

list = ['Beetechnical', 'is','1','technical','site']

list.insert(3,'Great')       # to insert element at desired index
print(list)

list.sort()                 # to sort elements inside list
print(list)

list.reverse()              # to reverse sorted or unsorted elements
print(list)

list.remove('1')            # to remove unwanted elements
print(list)

list.clear()               #to clear all elements of list
print(list)

Output :

List function in Python
List function
  • In the beginning, insert() was used to insert objects where needed and output them.
  • Following that, the corresponding functions run one by one and output the modifications made to the list in the specified order.

Code (Tuple) :

# tuple doesn't supported functios

tuple = 'Beetechnical', 'is','1','technical','site'

tuple.insert(3,'Great')       # to insert element at desired index
print(tuple)

tuple.sort()                 # to sort elements inside list
print(tuple)

tuple.reverse()              # to reverse sorted or unsorted elements
print(tuple)

tuple.remove('1')            # to remove unwanted elements
print(tuple)

tuple.clear()            
print(tuple)

Output :

List and Tuple | Detailed Discussion | Python | 2022 4
Error output for tuple function

The code above demonstrates that due to tuple’s lack of support for certain functions, an attribute error occurs.

Packing and UnPacking in Python

Packing entails gathering all elements into a single variable, but unpacking entails assigning separate elements to various variables. Both enable packing, whereas only Tuple supports unpacking.

Code :

list = ['Beetechnical', 'is','1','technical','site']
 
a,b,*d =  ['Beetechnical', 'is','1','technical','site']    #  * is for storing remaining elements

print(b)
print(d)

# tuple unpacking
tuple = 'Beetechnical', 'is','1','technical','site'

a,b,c,d,e='Beetechnical', 'is','1','technical','site'
 
print(a)
print(c)

Output :

How to use Packing and UnPacking in python
Output for packing

Conclusion :

The difference between a tuple and a list is now clear, as is where and when they can be used, as well as the supported and unsupported functions.

Finally, we can argue that in order to make our program efficient, we should use a list or tuple depending on the program’s needs, i.e.,

if we want to alter our array at runtime, we can prefer a list, and if we don’t want unexpected changes, we should use a tuple because it is space-efficient.

Comments are closed.

Scroll to Top