Python Collections: Lists, Tuples, Sets, and Dictionaries

Python offers a variety of built-in data structures to manage collections of data. Each comes with unique characteristics and operations that make them suitable for different types of tasks. This blog will explore Lists, Tuples, Sets, and Dictionaries, diving into their features, uses, and the operations you can perform on them.

Lists

Characteristics

  • Square brackets: Defined using square brackets [].
  • Ordered elements: Elements are stored in a specific order.
  • Duplicates allowed: Can contain duplicate elements.
  • Different datatypes: Can store elements of different datatypes.
  • Mutable: Elements can be changed after the list is created.
  • Packing: Assigning values to a list.
  • Unpacking: Extracting values back into variables
  • When the number of elements in a list is unknown, an asterisk * can be used to assign the remaining elements to a variable as a list.

Operations

  • append(): Adds an element to the end.
  • clear(): Deletes all elements.
  • copy(): Copies the entire list.
  • count(): Returns the number of occurrences of an element.
  • extend(): Adds all elements of another list to the end.
  • insert(): Inserts an element at a specified index.
  • remove(): Removes the first occurrence of an element.
  • pop(): Removes and returns an element at a specified index.
  • reverse(): Reverses the list.
  • sort(): Sorts the list in ascending order (elements must be of the same type).
  • len(): Returns the number of elements.
  • index(): Finds the index of an element.
  • Indexing and Slicing: Accessing parts of the list.
  • Adding two lists: Combines lists using the + operator


OUTPUT:

Tuples

Characteristics

  • Open brackets: Defined using parentheses ().
  • Ordered elements: Elements are stored in a specific order.
  • Duplicates allowed: Can contain duplicate elements.
  • Different datatypes: Can store elements of different datatypes.
  • Immutable: Elements cannot be changed after the tuple is created.

Operations

  • count(): Returns the number of occurrences of an element.
  • Indexing and Slicing: Accessing parts of the tuple.
  • Adding two tuples: Combines tuples using the + operator.

OUTPUT:

Sets

Characteristics

  • Different datatypes: Can store elements of different datatypes.
  • Unique elements: Only unique elements are allowed.
  • Unordered: Elements appear in a random or sorted order.
  • Mutable: Elements can be added or removed.

Operations

  • add(): Adds an element to the set.
  • clear(): Removes all elements from the set.
  • pop(): Removes and returns a random element.
  • intersection(): Returns elements common to both sets.
  • issuperset(): Checks if a set contains all elements of another set.
  • difference(): Returns elements in one set but not in the other.
  • discard(): Removes a specified element without error if the element is not found.
  • remove(): Removes a specified element and raises an error if the element is not found.
  • difference_update(): Removes elements found in another set from the original set.
  • intersection_update(): Updates the set with elements common to both sets.
  • is_disjoint(): Checks if two sets have no elements in common.
  • Cannot add two sets: Sets do not support the + operator.

OUTPUT:

Dictionaries

Characteristics

  • Key-value pairs: Stores data in key-value pairs.
  • Mutable: Key-value pairs can be added or removed.
  • Cannot add two dictionaries: Dictionaries do not support the + operator.

Operations

  • pop(key): Removes and returns the value for the specified key.
  • keys(): Returns a list of keys.
  • values(): Returns a list of values.
  • fromkeys(keys, value): Creates a dictionary with specified keys and value.
  • get(key): Returns the value for the specified key.
  • popitem(): Removes and returns the last key-value pair.
  • update({key: value}): Updates the dictionary with the specified key-value pairs.
  • setdefault(key, value): Returns the value for the specified key if it exists; otherwise, inserts the key with the specified value.

OUTPUT:




Popular posts from this blog

Operators in Python

Exploring the Depths of Python: Functions, Recursion, and the Power of Lambda Expressions