SoFunction
Updated on 2025-05-15

Understand the difference between Python List and Tuple

1. Basic concepts of List and Tuple

  • List (list): is a built-in data type in Python. It is an ordered collection that can add and delete elements in it at any time.
  • Tuple (tuple): It is also a built-in data type in Python. An immutable ordered collection, once created, the elements cannot be changed.

Sample code:

my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
  • Note: When only one element is included in the tuple, you need to add a comma after the element.my_tuple=(123,), If no comma is added, the created one is not a tuple, but refers to123This number is here. This is because brackets () can represent both tuples and small brackets in mathematical formulas, which creates ambiguity.

2. List of core differences

characteristic List (list) Tuple (tuple)
Variability variable Immutable
grammar [1, 2, 3] (1, 2, 3)
Takes up memory Larger Smaller
speed Slower Faster
Is it hashable no yes
Applicable scenarios Need to modify the data No need to modify data

3. High-efficiency usage of List and Tuple

1. List's efficient operation

  • Add elements in batches
my_list.extend([4, 5, 6])
  • List comprehension
squares = [x**2 for x in range(10)]
  • Delete elements
my_list.remove(2)  # Delete an element with a value of 2del my_list[0]     # Delete the first element
  • Common functions & methods

Functions & Methods describe
len(list) Number of list elements
max(list) Returns the maximum value of the list element
min(list) Returns the minimum value of the list element
list(seq) Convert tuples to list
(obj) Add new object at the end of the list
(obj) Count the number of times an element appears in the list
(seq) Append multiple values ​​from another sequence at one time at the end of the list (extend the original list with the new list)
(obj) Find the index position of the first match of a value from the list
(index, obj) Insert objects into list
(obj=list[-1]) Removes an element in the list (the last element by default) and returns the value of that element
(obj) Removes an element in the list (the parameter is an element in the list) and does not return any value
() Reverse list elements
([func]) Sort the original list

2. Tuple's hidden advantages

  • Key as dictionary
my_dict = {('x', 1): 'value1', ('y', 2): 'value2'}
  • Unpacking and assignment
a, b, c = (1, 2, 3)
  • Function returns multiple values
def get_point():
    return (3, 4)
x, y = get_point()

4. Performance comparison between List and Tuple

Tuple access speed is faster than List! Because Tuple is immutable, Python can optimize it more.

Performance Test:

import timeit

print((stmt="[1,2,3,4,5]", number=1000000))
print((stmt="(1,2,3,4,5)", number=1000000))

5. Should we use List or Tuple?

  • Need to modify the data frequently?Use List!
  • Data will not change, pursue performance and security?Use Tuple!
  • Want to be a dictionary key?Only use Tuple!

6. Details you may ignore

  • A single element Tuple should be added with a comma:(1,)
  • Tuple can nest List, but List cannot be used as a key for a dictionary

Summarize

This is the end of this article about understanding the difference between Python List and Tuple. For more information about the differences between Python List and Tuple, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!