Four data structures in Python

Four data structures in Python

List:

Mutable: You can change, add, or remove items after the list creation.

Ordered: The order of items is maintained, and items can be accessed by their index.

Syntax: Created using square brackets [] …


This content originally appeared on DEV Community and was authored by Syed Sadat Ali

Four data structures in Python

  • List:

    • Mutable: You can change, add, or remove items after the list creation.
    • Ordered: The order of items is maintained, and items can be accessed by their index.
    • Syntax: Created using square brackets [] or the list() function.
    • Duplicates: Allows duplicate elements.
      • Example: [1, 2, 3, 'apple', 'banana']
  • Tuple:

    • Immutable: Once created, you cannot change, add, or remove items.
    • Ordered: Like lists, the order is maintained, and index access is possible.
    • Syntax: Created using parentheses () or the tuple() function.
    • Duplicates: Allows duplicate elements.
      • Example: (1, 2, 3, 'apple', 'banana')
  • Set:

    • Mutable: You can add or remove items, but you cannot change individual items.
    • Unordered: No index access because sets do not record element position.
    • Syntax: Created using curly braces {} or the set() function.
    • Duplicates: Does not allow duplicate elements; only unique items.
      • Example: {1, 2, 3, 'apple', 'banana'}
  • Dictionary:

    • Mutable: You can change, add, or remove items, which are key-value pairs.
    • Ordered: As of Python 3.7, dictionaries maintain insertion order.
    • Syntax: Created using curly braces {} with key-value pairs or the dict() function.
    • Duplicates: Keys must be unique, but values can be duplicated.
      • Example: {1: 'apple', 2: 'banana', 3: 'cherry'}

Quick summary in code:

# List: Mutable, ordered, allows duplicates
my_list = [1, 2, 2, 'apple']

# Tuple: Immutable, ordered, allows duplicates
my_tuple = (1, 2, 2, 'apple')

# Set: Mutable, unordered, no duplicates
my_set = {1, 2, 'apple'}

# Dictionary: Mutable, ordered (since Python 3.7), unique keys
my_dict = {1: 'apple', 2: 'banana', 3: 'apple'}

Each structure has its own use-cases depending on the requirements of mutability, order, and uniqueness in your Python program.


This content originally appeared on DEV Community and was authored by Syed Sadat Ali


Print Share Comment Cite Upload Translate Updates
APA

Syed Sadat Ali | Sciencx (2024-09-07T20:45:36+00:00) Four data structures in Python. Retrieved from https://www.scien.cx/2024/09/07/four-data-structures-in-python/

MLA
" » Four data structures in Python." Syed Sadat Ali | Sciencx - Saturday September 7, 2024, https://www.scien.cx/2024/09/07/four-data-structures-in-python/
HARVARD
Syed Sadat Ali | Sciencx Saturday September 7, 2024 » Four data structures in Python., viewed ,<https://www.scien.cx/2024/09/07/four-data-structures-in-python/>
VANCOUVER
Syed Sadat Ali | Sciencx - » Four data structures in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/07/four-data-structures-in-python/
CHICAGO
" » Four data structures in Python." Syed Sadat Ali | Sciencx - Accessed . https://www.scien.cx/2024/09/07/four-data-structures-in-python/
IEEE
" » Four data structures in Python." Syed Sadat Ali | Sciencx [Online]. Available: https://www.scien.cx/2024/09/07/four-data-structures-in-python/. [Accessed: ]
rf:citation
» Four data structures in Python | Syed Sadat Ali | Sciencx | https://www.scien.cx/2024/09/07/four-data-structures-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.