Python List - Access Items - list[index], list[range]


Python List - Access Items

To access list items individually, you can use an index just like an array. You can also access a range of items in the list by specifying a range as an index.

In this tutorial, we will go through examples to understand how to access items in a Python list step-by-step.


Examples

1: Access a single item in a given list using index

In this example, we access a single item from the list using an index.

Python Program

my_list = [52, 85, 'apple', 'banana']

# Access 3rd item whose index=2
item = my_list[2]

print(item)

Output

apple

Explanation (Step-by-Step):

  1. Define a list my_list with four elements: [52, 85, 'apple', 'banana'].
  2. Use my_list[2] to access the third element of the list. The index starts from 0, so index 2 corresponds to the third item.
  3. Assign the accessed element 'apple' to the variable item.
  4. Print the value of item, which is 'apple'.

2: Access a range of items in a list using slicing

Here, we use slicing to retrieve a subset of the list.

Python Program

my_list = [52, 85, 41, 'apple', 'banana']

# Access a range of items
sub_list = my_list[1:4]

print(sub_list)

Output

[85, 41, 'apple']

Explanation (Step-by-Step):

  1. Define a list my_list with five elements: [52, 85, 41, 'apple', 'banana'].
  2. Use slicing my_list[1:4] to retrieve a sublist. The start index is 1, and the stop index is 4 (exclusive).
  3. The slice includes the items at indices 1, 2, and 3, which are 85, 41, and 'apple', respectively.
  4. Assign the sublist [85, 41, 'apple'] to the variable sub_list.
  5. Print the value of sub_list.

3: Access items with negative indexing

Negative indexing allows you to access elements from the end of the list.

Python Program

my_list = [52, 85, 41, 'apple', 'banana']

# Access the last item
last_item = my_list[-1]

# Access the second-to-last item
second_last_item = my_list[-2]

print(last_item)
print(second_last_item)

Output

banana
apple

Explanation (Step-by-Step):

  1. Define a list my_list with five elements: [52, 85, 41, 'apple', 'banana'].
  2. Use my_list[-1] to access the last element, 'banana', and assign it to last_item.
  3. Use my_list[-2] to access the second-to-last element, 'apple', and assign it to second_last_item.
  4. Print the values of last_item and second_last_item.

4: Access every nth item using slicing

Slicing with a step value allows you to retrieve every nth element.

Python Program

my_list = [10, 20, 30, 40, 50, 60]

# Access every 2nd item
every_second_item = my_list[::2]

print(every_second_item)

Output

[10, 30, 50]

Explanation (Step-by-Step):

  1. Define a list my_list with six elements: [10, 20, 30, 40, 50, 60].
  2. Use slicing my_list[::2]. Here, the step value 2 means select every second element starting from the beginning.
  3. The slice includes 10, 30, and 50.
  4. Assign the resulting sublist [10, 30, 50] to the variable every_second_item.
  5. Print the value of every_second_item.

5: Access items using a loop and condition

Here, we use a loop and condition to filter items from the list.

Python Program

my_list = [1, 2, 3, 4, 5, 6]

# Access even numbers
even_numbers = [item for item in my_list if item % 2 == 0]

print(even_numbers)

Output

[2, 4, 6]

Explanation (Step-by-Step):

  1. Define a list my_list with six elements: [1, 2, 3, 4, 5, 6].
  2. Use a list comprehension to iterate through each element in my_list.
  3. For each item, check if it is divisible by 2 (even number).
  4. Include the items 2, 4, and 6 in the new list even_numbers.
  5. Print the value of even_numbers.

Summary

In this tutorial, we learned how to access individual elements, slices, and filtered elements from a Python list using indexes, slicing, and conditions with step-by-step explanations.




Python Libraries