In this article List Tuple Dictionary and Set in Python, we give the information about List, Tuple, Dictionary, and Set are different types of data structures used for storing collections of data.

Difference between List Tuple Dictionary and Set in Python:

  1. List (list)

  • Ordered: Elements are stored in a specific order.
  • Mutable: Can be modified (add, remove, or update elements).
  • Duplicates Allowed: Can contain duplicate values.
  • Indexed: Supports indexing and slicing.
  • Syntax: [] (square brackets)

Example:

my_list = [1, 2, 3, 4, 2]

my_list.append(5)   # Adds 5 to the list

print(my_list)  # Output: [1, 2, 3, 4, 2, 5]

  1. Tuple (tuple)

  • Ordered: Elements have a fixed order.
  • Immutable: Cannot be modified after creation.
  • Duplicates Allowed: Can contain duplicate values.
  • Indexed: Supports indexing and slicing.
  • Syntax: () (parentheses)

Example:

my_tuple = (1, 2, 3, 4, 2)

print(my_tuple[1])  # Output: 2

Immutable nature:

my_tuple[1] = 10  # Error: Tuples do not support item assignment

  1. Dictionary (dict)

  • Unordered (Python 3.6 and below), Ordered (Python 3.7+): Maintains insertion order.
  • Mutable: Can be modified (add, update, or remove key-value pairs).
  • No Duplicates in Keys: Keys must be unique, but values can be duplicated.
  • Key-Value Pair Storage: Uses {key: value} structure.
  • Syntax: {} (curly braces)

Example:

my_dict = {“name”: “Alice”, “age”: 25, “city”: “New York”}

my_dict[“age”] = 26  # Updating value

print(my_dict)  # Output: {‘name’: ‘Alice’, ‘age’: 26, ‘city’: ‘New York’}

  1. Set (set)

  • Unordered: Does not maintain order.
  • Mutable: Can add or remove elements.
  • No Duplicates: Only unique values are stored.
  • Does NOT Support Indexing: Elements cannot be accessed by index.
  • Syntax: {} (curly braces), but with only values (no key-value pairs)

Example:

my_set = {1, 2, 3, 4, 2}

print(my_set)  # Output: {1, 2, 3, 4} (duplicates removed)

my_set.add(5)  # Adds 5

my_set.remove(2)  # Removes 2

print(my_set)  # Output: {1, 3, 4, 5}

Comparison Table

Feature List (list) Tuple (tuple) Dictionary (dict) Set (set)
Ordered Yes Yes (Python 3.7+) No
Mutable Yes No Yes Yes
Duplicates Allowed Yes Yes No (Keys must be unique) No
Indexed Access Yes Yes (By key) No
Syntax [] () {key: value} {}
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *