Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 7

Explain the difference between a list and a tuple.

Answer:

Lists and tuples are both sequence data types in Python that can store collections of items. However, there are several key differences between them:

Mutability

  • List: Lists are mutable, which means you can change their content (add, remove, or modify items) after the list has been created.

    my_list = [1, 2, 3]
    my_list[0] = 10     # Modifying an element
    my_list.append(4)   # Adding an element
    print(my_list)      # Output: [10, 2, 3, 4]
  • Tuple: Tuples are immutable, which means once a tuple is created, you cannot change its content (no adding, removing, or modifying items).

    my_tuple = (1, 2, 3)
    # my_tuple[0] = 10  # This will raise a TypeError
    print(my_tuple)    # Output: (1, 2, 3)

Syntax

  • List: Lists are defined using square brackets [].

    my_list = [1, 2, 3]
  • Tuple: Tuples are defined using parentheses (). However, a tuple with a single element requires a trailing comma.

    my_tuple = (1, 2, 3)
    single_element_tuple = (1,)  # Note the trailing comma

Performance

  • List: Lists have a variable size, and because they are mutable, operations that modify the list can be slower due to potential resizing or reallocation.

    my_list = [1, 2, 3, 4]
  • Tuple: Tuples have a fixed size, and since they are immutable, they are generally faster than lists in operations where immutability is a benefit, such as iterating through elements.

    my_tuple = (1, 2, 3, 4)

Use Cases

  • List: Use lists when you need a mutable sequence that can change over time, such as when collecting user input or maintaining a collection that may grow or shrink.

    user_input_list = []
    user_input_list.append(input("Enter a value: "))
  • Tuple: Use tuples when you need an immutable sequence to ensure that data cannot be altered. Tuples are often used for fixed collections of items, such as coordinates or configuration constants.

    coordinates = (10.0, 20.0)

Methods and Functions

  • List: Lists have several built-in methods that allow for modification, such as append(), extend(), insert(), remove(), pop(), clear(), sort(), and reverse().

    my_list = [1, 2, 3]
    my_list.append(4)
    my_list.remove(2)
  • Tuple: Tuples have fewer methods because they are immutable. The main methods available are count() and index().

    my_tuple = (1, 2, 3, 2)
    print(my_tuple.count(2))  # Output: 2
    print(my_tuple.index(3))  # Output: 2

Examples

  • List Example:

    fruits = ["apple", "banana", "cherry"]
    fruits.append("date")
    print(fruits)  # Output: ['apple', 'banana', 'cherry', 'date']
  • Tuple Example:

    fruits = ("apple", "banana", "cherry")
    print(fruits[1])  # Output: 'banana'

Summary

  • Mutability: Lists are mutable; tuples are immutable.
  • Syntax: Lists use square brackets []; tuples use parentheses ().
  • Performance: Tuples can be more performant due to immutability.
  • Use Cases: Lists are for dynamic collections; tuples are for fixed collections.
  • Methods: Lists have more methods for modification; tuples have fewer methods.

Understanding these differences helps you choose the appropriate data type for your specific use case, ensuring efficient and effective use of Python’s capabilities.

Recent job openings