Indexing and Slicing in Python Lists Read

Indexing and Slicing in Python Lists Read

Indexing and slicing are fundamental operations to access and extract elements from a list in Python.


1. Indexing (Accessing Single Elements)

  • Lists are zero-indexed (first element is at index 0).
  • Negative indices count from the end (-1 = last element).

Example 1: Basic Indexing

python

fruits = ["apple", "banana", "cherry", "date", "fig"]

# Positive indexing
print(fruits[0])   # "apple" (1st element)
print(fruits[2])   # "cherry" (3rd element)

# Negative indexing
print(fruits[-1])  # "fig" (last element)
print(fruits[-3])  # "cherry" (3rd from the end)

Output:

text

apple
cherry
fig
cherry

2. Slicing (Extracting a Sub-List)

  • Syntax: list[start:stop:step]
    • start (inclusive), stop (exclusive), step (optional, default=1).
  • Omitting start or stop slices from the start/end.

Example 2: Basic Slicing

python

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Get elements from index 2 to 5 (exclusive)
print(numbers[2:5])   # [2, 3, 4]

# From start to index 4
print(numbers[:4])    # [0, 1, 2, 3]

# From index 6 to end
print(numbers[6:])    # [6, 7, 8, 9]

# Every 2nd element
print(numbers[::2])   # [0, 2, 4, 6, 8]

# Reverse the list
print(numbers[::-1])  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Output:

text

[2, 3, 4]
[0, 1, 2, 3]
[6, 7, 8, 9]
[0, 2, 4, 6, 8]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Similar Posts

  • Method Overloading

    Python does not support traditional method overloading in the way languages like C++ or Java do. If you define multiple methods with the same name, the last definition will simply overwrite all previous ones. However, you can achieve the same result—making a single method behave differently based on the number or type of arguments—using Python’s…

  • Generalization vs. Specialization

    Object-Oriented Programming: Generalization vs. Specialization Introduction Inheritance in OOP serves two primary purposes: Let’s explore these concepts with clear examples. 1. Specialization (Extending Functionality) Specialization involves creating a new class that inherits all features from a parent class and then adds new, specific features. The core idea is reusability—you build upon what already exists. Key Principle: Child Class =…

  • Formatting Date and Time in Python

    Formatting Date and Time in Python Python provides powerful formatting options for dates and times using the strftime() method and parsing using strptime() method. 1. Basic Formatting with strftime() Date Formatting python from datetime import date, datetime # Current date today = date.today() print(“Date Formatting Examples:”) print(f”Default: {today}”) print(f”YYYY-MM-DD: {today.strftime(‘%Y-%m-%d’)}”) print(f”MM/DD/YYYY: {today.strftime(‘%m/%d/%Y’)}”) print(f”DD-MM-YYYY: {today.strftime(‘%d-%m-%Y’)}”) print(f”Full month: {today.strftime(‘%B %d, %Y’)}”) print(f”Abbr…

  • Alternation and Grouping

    Complete List of Alternation and Grouping in Python Regular Expressions Grouping Constructs Capturing Groups Pattern Description Example (…) Capturing group (abc) (?P<name>…) Named capturing group (?P<word>\w+) \1, \2, etc. Backreferences to groups (a)\1 matches “aa” (?P=name) Named backreference (?P<word>\w+) (?P=word) Non-Capturing Groups Pattern Description Example (?:…) Non-capturing group (?:abc)+ (?i:…) Case-insensitive group (?i:hello) (?s:…) DOTALL group (. matches…

  •  List operators,List Traversals

    In Python, lists are ordered, mutable collections that support various operations. Here are the key list operators along with four basic examples: List Operators in Python 4 Basic Examples 1. Concatenation (+) Combines two lists into one. python list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list1 + list2 print(combined) # Output: [1, 2, 3,…

Leave a Reply

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