×

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data types that are used to store a collection of values. The other two built-in types are set and dictionary. These are sequential data types, meaning you can iterate over the elements stored in them. It is so important to be able to differentiate between these data types.

Python List

Lists are one of the built-in data types in Python. Lists are primarily used to store multiple values of the same or different data types in a single variable. In other words, Lists are used for storing a collection of data.

Lists don’t have to be homogenous, so they can store different types of values, which is a very powerful feature in a programming language. Lists are dynamically sized arrays, so we can increase or decrease their size per our requirement.

Lists are created using a square bracket [ ], and the data Inside it is separated using a comma (,).

Syntax:

# creating list
list_name = [data1, data2, data3,…..]

Example:

#  creating a list of integer
Student_age = [ 15, 20, 30, 40 ]


# creating a list of mixed data types like integer, float and string
my_var = [ 1,  ‘TutorialAndExample’ , 5.6 ]

Explanation:

In the above example, we have created a list with the variable name Student_age and assigned it values which are of the same data type integer. Also, we have created a list of mixed data types with the variable name my_var and assigned it an integer value, a string value and a string value.

Accessing Elements in a List:

We use the index operator [ ] to access elements of a list. To access the first element of a list, we will access it using index 0.

Let’s understand with an example.

# creating lists
list_name = [ 1, 2.5, ‘ Hi! ’ ]


#accessing the elements of lists
print( list_name[0] )     #  This line will print 1
print( list_name[1] )     # This line will print 2.5
print( list_name[2] )     # This line will print Hi!

Explanation:

In the above example, we have created a list of name list_name and stored values of mixed data type. After that, we accessed those data types using the output function print and the access operator ( [ ] ).

Python Tuples

Python tuples are also used for storing multiple values of one or more than one data type in a single variable. Tuples are used to store a collection of ordered data which can’t be changed. Tuples don’t have to be homogenous, same as a list. Tuples allow duplicate values (more than one similar data) to store in them.

Tuples are created using parenthesis (), and elements are separated using comma (,). However, parentheses are optional in the case of tuples. We can create tuples with or without a pair of parentheses. Creating tuples without parentheses is known as tuple packing.

Syntax:

# creating tuples using parentheses
tuple_name = (‘data1’, ‘data2’, ‘data3’, ‘data4’)


# creating tuples without using parentheses
# tuple packing 
tuple_name = data1 , data2, data3

Example:

# Different types of tuple


# empty tuple
first_tuple = ( )
print (first_tuple)


# tuple of integers
first_tuple = ( 1,2,3,4 )
print (first_tuple)


# tuple with mixed data types
first_tuple = ( 1, ‘A’, 2.5)
print (first_tuple)


# nested tuple
first_tuple = (“TutorialAndExample”, [ 9,10,11 ] , (1,2,3) )
print(first_tuple)


Outputs:

( )
( 1, 2, 3, 4 )
( 1, ‘A’, 2.5 )
(‘TutorialAndExample’, [ 9, 10, 11 ], ( 1, 2,3 ))

Explanation:

The above code shows the different ways to create a tuple and print its value.

Difference between List and Tuple

Syntax Difference

The syntax for creating a list and a tuple are different. Lists are created using square brackets. While on the other hand, tuples are created using parentheses which is optional. For example,

creating_list  =  [‘a’, ‘b’, ‘c’, ‘d’]
                 creating_tuple  =  ( ‘a’, ‘b’, ‘c’, ‘d’ )

Mutable Lists and Immutable Tuples

The most important difference between a list and a tuple is that lists are mutable, which means they can be changed or manipulated. In contrast, tuples are immutable, which means they can’t be changed or manipulated once they are created.

Iteration

Iterating a list is slower and more time-consuming, whereas iterating a tuple is faster.

Memory Consumption

Lists have more memory consumption compared to tuples. Since lists are mutable, they consume more memory space. Tuples operation has a smaller size than that of lists.

Usefulness

Lists are very useful for insertion and deletion operations, whereas tuples are very useful for read-only operations.

Methods

Lists provide more built-in functions as compared to tuples. To get all the associated built-in functions related to lists and tuples, we use the dir([objects])  command.

Security

List elements are more prone to getting errors while tuples operations are safer.


Related Topics

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.