• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Tuple vs List in Python: Syntax, Definition, and Performance

Tuple vs List in Python: Syntax, Definition, and Performance

Author: Aditya Raj
Last Updated: May 10, 2023

In Python, tuples and lists are similar data structures apart from mutability. This article discusses Python tuple vs list to compare the syntax, definition, mutability, and performance of both data structures. 

Table of Contents
  1. Tuple vs List Definition
  2. Tuple vs List Syntax
  3. Tuple vs List Mutability
  4. Python Tuple vs List Performance
  5. When to Use Tuple vs List in Python?
  6. Conclusion

Tuple vs List Definition

A Python list is a mutable data structure that we use to store elements in a sequential manner. We use a list to store data when we need random access to the elements.

A tuple is an immutable data structure that we use to store elements. A tuple in Python has all the properties of a list except mutability. 

Tuple vs List Syntax

Tuple and List have different syntaxes. We use square brackets to define a list. On the other hand, parentheses are used to define a tuple. You can observe this in the following example.

myList=[1,2,4,5,6,6,7,8,99,33,4324,2,672]
myTuple=(1,2,4,5,6,6,7,8,99,33,4324,2,672)

Similarly, we use the list() function to create a list whereas the tuple() function to create a tuple from an existing collection object such as a set. You can observe this in the following example.

mySet={1,2,3,4,5}
myList=list(mySet)
myTuple=tuple(mySet)

Tuple vs List Mutability

A python tuple is immutable whereas a list is mutable. If you try to change a tuple by any means, the program will run into an error.

  • We cannot add an element to a Python tuple. On the other hand, we can add elements as well as other container objects to an existing list. 
  • We can delete elements from a list. On the contrary, we cannot delete any element from an existing Python tuple.

Python Tuple vs List Performance

If we traverse a tuple and a list, traversing a tuple is more efficient than traversing a list. 

  • The performance of a tuple in this case is at least 10 percent better than a Python list.
  • A tuple has a fixed size. So, it contains occupies only the space needed for its elements. On the other hand, a list occupies almost double the space needed by its elements. This makes Python lists inefficient compared to tuples.

Considering the above points, we can say that a Python tuple has better performance than a list.

For example, 

  • Traversing a list containing 13 elements takes almost 209 ns ± 36 ns per loop when we do nothing except traverse the list.
  • Traversing a Python tuple with the same number of elements takes 191 ns ± 16.3 ns which is a considerably lower time. 

You can observe this in the following example.

Python Tuple vs List Performance
Python Tuple vs List Performance

When to Use Tuple vs List in Python?

If you don’t need to change the data in the sequence after defining it, you should use a Python tuple. If you need to add, update, or delete elements from a sequence, you should use a list in Python.

Conclusion

In this article, we had a discussion about tuple vs list in Python to have a look at the definition, syntax, and performance of both data structures. To learn more about Python programming, you can read this article on for loop vs while loop in Python. You might also like this article on if vs elif vs else if in Python.

I hope you enjoyed reading this article. Say tuned for more informative articles.

Happy learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us