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

  • Top Programming Languages and Tools Developed Using Python

    Python itself is not typically used to develop other programming languages, as it is a high-level language designed for general-purpose programming. However, Python has been used to create domain-specific languages (DSLs), tools for language development, and educational languages. Here are some examples: 1. Hy 2. Coconut Description: A functional programming language that compiles to Python. It adds…

  • Python timedelta Explained

    Python timedelta Explained timedelta is a class in Python’s datetime module that represents a duration – the difference between two dates or times. It’s incredibly useful for date and time arithmetic. Importing timedelta python from datetime import timedelta, datetime, date Basic Syntax python timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Examples 1. Basic timedelta Creation python from datetime…

  • Closure Functions in Python

    Closure Functions in Python A closure is a function that remembers values from its enclosing lexical scope even when the program flow is no longer in that scope. Simple Example python def outer_function(x): # This is the enclosing scope def inner_function(y): # inner_function can access ‘x’ from outer_function’s scope return x + y return inner_function…

  • re module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

Leave a Reply

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