🐍2.2List Indexing & Slicing in Python (Forward & Backward, Positive & Negative)

 

📌 List Indexing & Slicing in Python (Forward & Backward, Positive & Negative)

🔹 Introduction

In Python, lists are a fundamental data structure used to store multiple items in a single variable. To efficiently retrieve and manipulate elements within a list, we use indexing and slicing.

For the given list:

my_list = [1, 2, 3, 3, 4, 5, ("A", "B", "B", "C", "D"), [11, 12, 13, 1, 2, 3], "DIVYA", "SANKET", "VENU"]

Let's explore indexing and slicing in Python. 🚀


🔹 Indexing in Python

Indexing is used to access elements in a list using positive (forward) or negative (backward) indexing.

✅ Positive Indexing (Forward)

Index Element
0 1
1 2
2 3
3 3
4 4
5 5
6 ("A", "B", "B", "C", "D")
7 [11, 12, 13, 1, 2, 3]
8 "DIVYA"
9 "SANKET"
10 "VENU"

Accessing elements using positive indexing:

print(my_list[0])   # Output: 1
print(my_list[6])   # Output: ('A', 'B', 'B', 'C', 'D')
print(my_list[8])   # Output: DIVYA

✅ Negative Indexing (Backward)

Index Element
-11 1
-10 2
-9 3
-8 3
-7 4
-6 5
-5 ("A", "B", "B", "C", "D")
-4 [11, 12, 13, 1, 2, 3]
-3 "DIVYA"
-2 "SANKET"
-1 "VENU"

Accessing elements using negative indexing:

print(my_list[-1])   # Output: VENU
print(my_list[-5])   # Output: ('A', 'B', 'B', 'C', 'D')
print(my_list[-10])  # Output: 2

🔹 Slicing in Python

Slicing is used to extract a portion of the list using the syntax:

list[start:stop:step]
  • startStarting index (inclusive)
  • stopEnding index (exclusive)
  • stepStep size (default is 1)

✅ Forward Slicing (Positive Indices)

print(my_list[2:6])   # Output: [3, 3, 4, 5]   (Elements from index 2 to 5)
print(my_list[:4])    # Output: [1, 2, 3, 3]   (Start from 0 to index 3)
print(my_list[5:])    # Output: [5, ('A', 'B', 'B', 'C', 'D'), [11, 12, 13, 1, 2, 3], 'DIVYA', 'SANKET', 'VENU']

✅ Backward Slicing (Negative Indices)

print(my_list[-5:-2])   # Output: [('A', 'B', 'B', 'C', 'D'), [11, 12, 13, 1, 2, 3], 'DIVYA']
print(my_list[:-6])     # Output: [1, 2, 3, 3, 4, 5]
print(my_list[-4:])     # Output: [[11, 12, 13, 1, 2, 3], 'DIVYA', 'SANKET', 'VENU']

✅ Slicing with Steps

print(my_list[1:8:2])   # Output: [2, 3, 5, [11, 12, 13, 1, 2, 3]]   (Every 2nd element)
print(my_list[::-1])    # Output: Reversed list
print(my_list[::3])     # Output: [1, 3, 5, [11, 12, 13, 1, 2, 3]]

🔍 Accessing Nested Lists

Python lists can contain other lists or tuples as elements. To access elements inside nested lists, we use double indexing:

print(my_list[6][1])   # Output: B  (Accessing 2nd element in the tuple)
print(my_list[7][3])   # Output: 1  (Accessing 4th element in the nested list)

🔹 Common Mistakes & Tips

Out-of-Range Indexing:

  • Accessing an index that does not exist results in an IndexError.
print(my_list[20])  # IndexError: list index out of range

Forgetting the Step in Slicing:

  • The step argument helps skip elements. Use [::-1] for reversing the list. ✔ Nested Indexing:
  • When working with nested lists, ensure proper indexing.

🚀 Conclusion

Indexing allows retrieving elements using positive & negative indices. ✅ Slicing extracts parts of the list using the start:stop:step syntax. ✅ Nested lists require multiple levels of indexing.

Mastering indexing & slicing will make data manipulation in Python much easier! Would you like more **advanced list oper

Comments

Popular posts from this blog

🔥Apache Spark Architecture with RDD & DAG

🌐Filtering and Copying Files Dynamically in Azure Data Factory (ADF)

🌐End-to-End ETL Pipeline: MS SQL to MS SQL Using Azure Databricks