Skip to main content

Tuples Vs Lists on Python

Jose CerrejonLess than 1 minuteDeveloper

Tuples Vs Lists on Python

Tuples Vs Lists
Tuples Vs Lists. Generated with AI.

tuples and lists are two types of data incorporated in Python that are used to store collections of elements. Although they share some similarities, they also have key differences. Let’s look at them:

  1. Changeability:

    • Tuples: They are immutable, which means that once created, they cannot be modified. You cannot add, delete, or change items in a tuple after it is created.
    • Lists: They are mutable, allowing you to add, delete and modify items at any time.
  2. Syntax:

    • Tuples: They are created by surrounding the elements in parentheses ().
    • Lists: Created using square brackets [].
  3. Use of commas:

    • Tuples: If you create a tuple with a single element, you must add a comma at the end. For example: (28,).
    • Lists: You do not need to add a comma at the end when you create a list with a single element.
  4. Efficiency:

    • Tuples: They are more efficient in terms of memory and performance than lists.
    • Lists: They can occupy more space and be less efficient due to their changeability.

So use tuples or lists depending on the changeability you need. Both have their specific use cases and can be useful in different situations.