Python Access Array Items

Python – Accessing Array Items

In Python, arrays are called lists. Lists are a versatile data structure that allows you to store and access multiple items in a single variable. Each item in a list is assigned a unique index, starting from 0 for the first item.

Accessing Items by Index

To access a specific item in a list, you can use its index. The index is enclosed in square brackets and follows the name of the list. Here’s an example:

my_list = ['apple', 'banana', 'cherry']
print(my_list[0])  # Output: apple
print(my_list[1])  # Output: banana
print(my_list[2])  # Output: cherry

In this example, we have a list called my_list that contains three items: ‘apple’, ‘banana’, and ‘cherry’. We access each item by its index using the [] operator. The first item has an index of 0, the second item has an index of 1, and so on.

Accessing Items by Negative Index

In addition to positive indices, Python also supports negative indices to access items from the end of a list. The last item has an index of -1, the second-to-last item has an index of -2, and so on. Here’s an example:

my_list = ['apple', 'banana', 'cherry']
print(my_list[-1])  # Output: cherry
print(my_list[-2])  # Output: banana
print(my_list[-3])  # Output: apple

Using negative indices can be useful when you want to access items from the end of a list without knowing its length.

Slicing Lists

Python allows you to access a range of items in a list using slicing. Slicing is done by specifying the start and end indices, separated by a colon. The start index is inclusive, and the end index is exclusive. Here’s an example:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(my_list[1:4])  # Output: ['banana', 'cherry', 'date']

In this example, we use slicing to access a range of items from index 1 to index 3 (excluding index 4). The result is a new list containing the sliced items.

You can also omit the start or end index to slice from the beginning or until the end of the list, respectively. Here are a few examples:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(my_list[:3])   # Output: ['apple', 'banana', 'cherry']
print(my_list[2:])   # Output: ['cherry', 'date', 'elderberry']
print(my_list[:])    # Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

By omitting both the start and end indices, you get a complete copy of the original list.

Accessing Items with Step Size

You can also specify a step size when slicing a list. The step size determines the number of items to skip between each item in the slice. Here’s an example:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
print(my_list[0:5:2])  # Output: ['apple', 'cherry', 'elderberry']

In this example, we specify a step size of 2, which means we include every second item in the slice. As a result, we get a new list containing ‘apple’, ‘cherry’, and ‘elderberry’.

Conclusion

Accessing array items in Python is straightforward using the index notation. Whether you need to access a single item, a range of items, or skip items using a step size, Python provides the necessary tools to manipulate lists efficiently.

Scroll to Top