List Indexing
As a Python list is an ordered sequence, each item is assigned the index position starting with 0. We can access each item by passing the index position in the square brackets.
Example:
emp_details = [‘Brad’,’Lee’,’Sales’,380,250000,True,’New York’,’USA’,’Developer’,’Python’]
Index Positions:
Let us understand with some examples:
emp_details = ['Brad','Lee','Sales',380,250000,True]
Check what is at position 0.
print(emp_details[0])
Position 4.
print(emp_details[4])
List Slicing
The slicing operation can extract a list of two or more than two items from the list. While performing slicing, we can pass three optional parameters in the square brackets separated by a colon.
List[StartPosition
: EndPosition
: Step
]
StartPosition | Item at which we want to start. Default is the first character. |
---|---|
EndPosition | Up to which item we want to go(Note: this is up to, the item at this specific location will not be included). Default is the last last. |
Step | The number of steps we want to it to take. By default, it is 1, means it will take one step at a time. |
When we don’t use a colon(:) then the argument is considered as the position of an item.
Let us understand index positions with some examples:
Only using start to end.
emp_details = ['Brad','Lee','Sales',380,250000,True,'New York','USA','Developer','Python']
print(emp_details[0:])
Start with 3 to end.
print(emp_details[3:])
Go up to 3. Note: Item at position 3 will not be included.
print(emp_details[:3])
Include all with step of 2.
print(emp_details[::2])
Start at 2 up to 8.
print(emp_details[2:8])
Start with 1 up to 8 with step of 3.
print(emp_details[1:8:3])
Negative Indexing
We can also use negative indexing. The last item is assigned with the index position of -1. It keeps on increasing in the negative order until the first item. If we want to traverse backward, we can use jump as a negative number. Example -1.
Example:
emp_details = [‘Brad’,’Lee’,’Sales’,380,250000,True,’New York’,’USA’,’Developer’,’Python’]
Negative Index Positions:
Let us understand with some examples:
Item at -2 position.
emp_details = ['Brad','Lee','Sales',380,250000,True,'New York','USA','Developer','Python']
print(emp_details[-2])
This will not work because the default jump size is 1 which is positive but to traverse backward we need a negative jump size.
print(emp_details[-2:-6])
Traversing from -2 uptill -6 with jump of -1.
print(emp_details[-2:-6:-1])
Traversing from -5 to -2 with a positive jump i.e. default 1.
print(emp_details[-5:-2])
We can use this approach to reverse the list.
print(emp_details[::-1])
Check if an item exists in List
To check whether an item exists inside the list or not, we use the membership operator in.
emp_details = ['Brad','Lee','Sales',380,250000,True]
if ("Brad" in emp_details):
print("Welcome, Brad!")
As Steve is not present, so nothing gets printed.
emp_details = ['Brad','Lee','Sales',380,250000,True]
if ("Steve" in emp_details):
print("Welcome, Steve!")
Index out of range error
If we try to access any index position where no item is present, then we get ‘index out of range’ error.
If we try to access item at index 2, we get index out of range error.
emp_details = ['Brad','Lee']
print(emp_details[2])
References
- Python List (With Examples)
- Python Program to Slice Lists
- Slicing and Indexing in Python – Explained with Examples