📚

Python Collections Overview

Jul 28, 2024

Python Collections

Introduction

  • Explanation of different types of collections in Python.
  • Focus on general-purpose collections: Lists, Sets, Tuples, and briefly Dictionaries.
  • Definition: Collection = single variable used to store multiple values.

Lists

  • Definition: A list is surrounded by square brackets [].
  • Example:
    • fruits = ['Apple', 'Orange', 'Banana', 'Coconut']
  • Naming Convention: Use plural for collection variables (e.g., fruits).

Accessing Elements

  • Use index operator: 0-based indexing.
  • Example:
    • fruits[0] returns 'Apple'.
    • fruits[1] returns 'Orange'.
    • Attempting to access an out-of-range index raises an error.

Slicing

  • : fruits[0:3] returns ['Apple', 'Orange', 'Banana'].
  • Step parameter: fruits[::2] returns every second element.
  • Reverse: fruits[::-1] returns list in reverse order.

Iteration

  • Use a for loop to iterate: for fruit in fruits:
  • Replace X with descriptive variable name for readability.

Methods and Functions

  • Use dir() to list methods: print(dir(fruits)).
  • Help function: help(fruits) provides descriptions for methods.
  • Common methods:
    • append(): Add an element.
    • remove(): Remove an element.
    • sort(): Sort the list.
    • clear(): Clear all elements.
  • Length: Use len(fruits) to return the number of elements.
  • Check for existence: if 'Apple' in fruits: returns a boolean.

Characteristics

  • Lists are ordered, changeable, and allow duplicates.
  • Example of modifying a list: fruits[0] = 'Pineapple'.

Sets

  • Definition: A set is surrounded by curly braces {}.
  • Example: fruits = {'Apple', 'Orange', 'Banana', 'Coconut'}.

Characteristics

  • Sets are unordered and immutable (cannot change values).
  • No duplicates allowed.

Methods

  • Use dir() for methods and help() for descriptions.

  • Common methods:

    • add(): Add an element.
    • remove(): Remove an element.
    • clear(): Clear all elements.
  • Length: Use len() to find out how many elements in a set.

  • Example codes:

    • Adding: fruits.add('Pineapple').
    • Removing: fruits.remove('Apple').

Characteristics Summary

  • Sets are unordered, immutable, and cannot have duplicates.

Tuples

  • Definition: A tuple is surrounded by parentheses ().
  • Example: fruits = ('Apple', 'Orange', 'Banana', 'Coconut').

Characteristics

  • Tuples are ordered and unchangeable but allow duplicates.
  • Faster than lists: Use when you need an ordered collection without changes.

Methods

  • Limited methods: count() and index().
  • Example: fruits.index('Apple') returns the index of 'Apple'.

Iteration

  • Tuples are iterable; you can use a for loop to iterate.

Summary of Collections

  1. Lists: Ordered, changeable, duplicates allowed.
  2. Sets: Unordered, immutable, duplicates not allowed.
  3. Tuples: Ordered, unchangeable, duplicates allowed, faster than lists.
  4. Dictionaries: To be discussed next.
  • Overall understanding: Collections in Python serve to group multiple values efficiently, each with its unique strengths and proper use cases.