gk nxt python book

Difference between a list and a tuple in Python

python interview questions
Free Python Book
Python Documentation PDF
Free Python Course
Difference between a tuple and a list
difference between a list and a tuple
    list
    • list is mutable
    • list is not efficient in memory usage
    • operations on a list are typically slower
    • list is not hashable, so cannot be used as a dictionary key
    • list is flexible (list has 11 methods)
    • list uses square brackets and usage of square brackets is mandatory
    tuple
    • tuple is immutable
    • tuple uses memory efficiently
    • operations on a tuple are typically faster
    • tuple can be a dict key if all its members are hashable
    • tuple isn't flexible (tuple has 2 methods)
    • tuple uses parentheses (except for an empty tuple, usage of parentheses is optional)
    A tuple’s memory size is fixed and don’t over-allocate. Thus, it can be stored more compactly than lists which need to over-allocate to make append() operations efficient.
    Tuples are stored in a single block of memory. Lists are allocated in two blocks: a fixed one with all the Python object information and a variable-sized block for the data.
    During execution, creating a tuple is faster than creating a list. References to objects are incorporated directly in a tuple object. In contrast, list has an extra layer of indirection to an external array of pointers. This gives tuples speed advantage for indexed lookups and unpacking. So, speed of iteration and indexing is slightly faster for tuples than lists. So, operations on tuples typically execute faster compared to operations on lists for large volumes of data.
    Lists have 11 built-in methods while tuples have only 2 built-in methods

    List methods
    1) append 2) clear 3) copy 4) count
    5) extend 6) index 7) insert 8) pop
    9) remove 10) reverse 11) sort

    Tuple methods
    1) count 2) index
python interview questions
Free Python Book
Python Documentation PDF
Free Python Course
GKNXT Encyclopedia
0 A B C D E
F G H I J K
L M N O P Q
R S T U V W
X Y Z #
colors list
Collective Nouns List
Top 10 Lists
World Flags