Negative Indexing
Negative indexing allows you to access elements in a sequence using negative indices. Negative indices start from -1 for the last element, -2 for the second-to-last, and so on.
Syntax
To access an element or perform slicing with negative indexing, you use the following syntax:
sequence[-index]
-index: A negative integer representing the position from the end of the sequence. For example, -1 refers to the last element, -2 to the second-to-last, and so on.
Detailed Examples
Accessing the Last Element
string = "Python Programming" last_char = string[-1] # Accesses the last character print(last_char) # Output: "g"
Here, -1 refers to the last character of the string.
Accessing the Second-to-Last Element
string = "Python Programming" second_last_char = string[-2] # Accesses the second-to-last character print(second_last_char) # Output: "n"
Here, -2 refers to the second character from the end.
Slicing with Negative Indexing
string = "Python Programming" # Slicing the last 5 characters last_five_chars = string[-5:] # Extracts the last 5 characters print(last_five_chars) # Output: "ming" # Slicing from the second-to-last character to the fifth-to-last character part = string[-5:-1] # Extracts from index -5 to -2 print(part) # Output: "ming"
-
- Here, string[-5:] extracts the last five characters of the string.
- string[-5:-1] extracts the characters from the fifth-to-last to the second-to-last.
Combining Negative Indexing with a Step
string = "abcdefgh" # Extracting characters in reverse order starting from the fifth-to-last reversed_part = string[-5::-1] # Extracts from index -5 to the beginning in reverse order print(reversed_part) # Output: "fedcba" # Extracting every second character from the second-to-last to the beginning step_part = string[-2::-2] # Extracts from index -2 to index 0, stepping by 2 print(step_part) # Output: "ceg"
-
- string[-5::-1] starts from the fifth-to-last character and extracts in reverse order until the beginning of the string.
- string[-2::-2] starts from the second-to-last character and extracts every second character in reverse order.
Edge Cases
-
- Negative Indexing with Empty Sequences:
empty_string = "" empty_slice = empty_string[-1:] # Empty string print(empty_slice) # Output: ""
With an empty string or list, using negative indices results in an empty string or list, respectively.
-
- Negative Indexing Beyond Length:
string = "Hello" slice_out_of_bounds = string[-10:] # Index -10 is beyond the length of the string print(slice_out_of_bounds) # Output: "Hello"
When the negative index is larger in absolute value than the length of the string, slicing starts from the beginning of the string.
Negative Indexing with Lists
Negative indexing also works similarly with lists.
numbers = [10, 20, 30, 40, 50] # Accessing the last element of the list last_element = numbers[-1] # 50 print(last_element) # Slicing the last 3 elements last_three_elements = numbers[-3:] # [30, 40, 50] print(last_three_elements) # Slicing from the fourth-to-last element to the second-to-last part = numbers[-4:-1] # [20, 30, 40] print(part)
-
- numbers[-1] accesses the last element of the list.
- numbers[-3:] extracts the last three elements.
- numbers[-4:-1] extracts from the fourth-to-last to the second-to-last element.
Summary
- Negative Indexing: Allows access to elements by counting from the end of the sequence, with -1 for the last element, -2 for the second-to-last, and so on.
- Negative Slicing: Allows you to slice sequences using negative indices for start and stop.
- Negative Step: You can also use negative steps to reverse the order of elements extracted.