Python Collections and Its Types (List, Tuple, Set, Dictionary)

 Python Collections :

  • Python collections are built-in data structures that allow you to store and manage groups of data. Each type of collection has its own unique properties and methods for storing, accessing, and manipulating data. 

There are four main types of collections in Python:

List:

  • Definition: A list is a collection that is ordered and changeable. It allows duplicate members.
  • Characteristics:
    • Allows duplicates.
    • Any type of data can be stored.
    • Can modify, add, or remove elements.
    • Methods: insert(), append(), extend(), pop()

# List Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(type(a))  # Output: <class 'list'>
a.append(10)
a.append(11)
print(a)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

# Append Example with Strings
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a.append("ten")
a.append("eleven")
print(a)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 'ten', 'eleven']

# Insert Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a.insert(0, 11)
print(a)  # Output: [11, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Modify Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a[0] = 11
print(a)  # Output: [11, 2, 3, 4, 5, 6, 7, 8, 9]

# Remove Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a.pop(0)
print(a)  # Output: [2, 3, 4, 5, 6, 7, 8, 9]

# Pop Last Element Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a.pop()
print(a)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]

# Extend Example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [10, 11, 12, 13, 14, 15]
a.extend(b)
print(a)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]



Tuple:

  • Definition: A tuple is a collection that is ordered and unchangeable (immutable). It allows duplicate members.
  • Characteristics:
    • Allows duplicates.
    • Any type of data can be stored.
    • Cannot modify or remove elements once created.
    • Can convert tuple to list for modifications.

# Tuple Example
a = (1, 2, 3, 4)
b = list(a)
b.pop()
print(a)  # Output: (1, 2, 3, 4) (Tuples cannot be changed)
print(b)  # Output: [1, 2, 3] (List is modified)

Set:

  • Definition: A set is a collection that is unordered, unchangeable (except for adding/removing), and unindexed. It does not allow duplicate members.
  • Characteristics:
    • No duplicates allowed.
    • Unordered collection.
    • Methods: add(), remove(), discard()

# Set Example
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(type(a))  # Output: <class 'set'>
a.add(10)
print(a)  # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

# Add Duplicate Example
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
a.add(1)
print(a)  # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9} (No change)

# Remove Example
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
a.remove(1)
print(a)  # Output: {2, 3, 4, 5, 6, 7, 8, 9}

# Discard Example
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
a.discard(1)
print(a)  # Output: {2, 3, 4, 5, 6, 7, 8, 9}

# Clear Example
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
a.clear()
print(a)  # Output: set()

Dictionary:

  • Definition: A dictionary is a collection that is unordered, changeable, and indexed. It consists of key-value pairs and does not allow duplicates.
  • Characteristics:
    • Keys must be unique.
    • Values can be of any type.
    • Methods: get(), keys(), values(), items()

# Dictionary Example
a = {
    "language": "Python",
    "Version": 3.11,
    "Creator": "Guido van Rossum",
    "Release Year": 1991,
    "Paradigm": ["Object-Oriented", "Procedural", "Functional"],
    "Website": "https://www.python.org/"
   }
print(a['language'])  # Output: Python
print(a['Release Year
']) # Output: 1991

# Add Example
a["Operating System"] =  ["Windows", "Linux", "macOS"]
print(a)  # Output: {'language': 'Python', 'Version': 3.11, 'Creator': 'Guido van Rossum', 'Release Year': 1991, 'Paradigm': ['Object-Oriented', 'Procedural', 'Functional'], 'Website': 'https://www.python.org/', 'Operating System': ['Windows', 'Linux', 'macOS']}

# Remove Example
del a['Website']
print(a) #{'language': 'Python', 'Version': 3.11, 'Creator': 'Guido van Rossum', 'Release Year': 1991, 'Paradigm': ['Object-Oriented', 'Procedural', 'Functional']}

# Keys Example
print(a.keys())  # Output: dict_keys(['name', 'city', 'gender'])

# Values Example
print(a.values())  # Output: dict_keys(['language', 'Version', 'Creator', 'Release Year', 'Paradigm', 'Website'])

# Items Example
print(a.items())  # Output: dict_values(['Python', 3.11, 'Guido van Rossum', 1991, ['Object-Oriented', 'Procedural', 'Functional'], 'https://www.python.org/'])

Post a Comment

0 Comments