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

  • replace(), join(), split(), rsplit(), and splitlines() methods in Python

    1. replace() Method Purpose: Replaces occurrences of a substring with another substring.Syntax: python string.replace(old, new[, count]) Examples: Example 1: Basic Replacement python text = “Hello World” new_text = text.replace(“World”, “Python”) print(new_text) # Output: “Hello Python” Example 2: Limiting Replacements (count) python text = “apple apple apple” new_text = text.replace(“apple”, “orange”, 2) print(new_text) # Output: “orange orange apple”…

  • Mathematical Functions

    1. abs() Syntax: abs(x)Description: Returns the absolute value (non-negative value) of a number. Examples: python # 1. Basic negative numbers print(abs(-10)) # 10 # 2. Positive numbers remain unchanged print(abs(5.5)) # 5.5 # 3. Floating point negative numbers print(abs(-3.14)) # 3.14 # 4. Zero remains zero print(abs(0)) # 0 # 5. Complex numbers (returns magnitude) print(abs(3 +…

  • Date/Time Objects

    Creating and Manipulating Date/Time Objects in Python 1. Creating Date and Time Objects Creating Date Objects python from datetime import date, time, datetime # Create date objects date1 = date(2023, 12, 25) # Christmas 2023 date2 = date(2024, 1, 1) # New Year 2024 date3 = date(2023, 6, 15) # Random date print(“Date Objects:”) print(f”Christmas:…

  • Indexing and Slicing for Writing (Modifying) Lists in Python

    Indexing and Slicing for Writing (Modifying) Lists in Python Indexing and slicing aren’t just for reading lists – they’re powerful tools for modifying lists as well. Let’s explore how to use them to change list contents with detailed examples. 1. Modifying Single Elements (Indexing for Writing) You can directly assign new values to specific indices. Example 1:…

  • re.split()

    Python re.split() Method Explained The re.split() method splits a string by the occurrences of a pattern. It’s like the built-in str.split() but much more powerful because you can use regex patterns. Syntax python re.split(pattern, string, maxsplit=0, flags=0) Example 1: Splitting by Multiple Delimiters python import retext1=”The re.split() method splits a string by the occurrences of a pattern. It’s like…

  • Python Calendar Module

    Python Calendar Module The calendar module in Python provides functions for working with calendars, including generating calendar data for specific months or years, determining weekdays, and performing various calendar-related operations. Importing the Module python import calendar Key Methods in the Calendar Module 1. calendar.month(year, month, w=2, l=1) Returns a multiline string with a calendar for the specified month….

Leave a Reply

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