30% Therapy – 40% Practice – 30% Work project

Python – Access List Items



Access List Items

In , a is a sequence of elements or objects, i.e. an ordered collection of objects. Similar to arrays, each element in a list corresponds to an index.

To access the values within a list, we need to use the square brackets “[]” notation and, specify the index of the elements we want to retrieve.

The index starts from 0 for the first element and increments by one for each subsequent element. Index of the last item in the list is always “length-1”, where “length” represents the total number of items in the list.

In addition to this, Python provides various other ways to access list items such as slicing, negative indexing, extracting a sublist from a list etc. Let us go through this one-by-one −

Accessing List Items with Indexing

As discussed above to access the items in a list using indexing, just specify the index of the element with in the square brackets (“[]”) as shown below −

mylist[4] 

Example

Following is the basic example to access list items −

list1 = ["Rohan", "Physics", 21, 69.75]
list2 = [1, 2, 3, 4, 5]

print ("Item at 0th index in list1: ", list1[0])
print ("Item at index 2 in list2: ", list2[2])

It will produce the following output −

Item at 0th index in list1: Rohan
Item at index 2 in list2: 3

Access List Items with Negative Indexing

Negative indexing in Python is used to access elements from the end of a list, with -1 referring to the last element, -2 to the second last, and so on.

We can also access list items with negative indexing by using negative integers to represent positions from the end of the list.

Example

In the following example, we are accessing list items with negative indexing −

list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]

print ("Item at 0th index in list1: ", list1[-1])
print ("Item at index 2 in list2: ", list2[-3])

We get the output as shown below −

Item at 0th index in list1: d
Item at index 2 in list2: True

Access List Items with Slice Operator

The slice operator in Python is used to fetch one or more items from the list. We can access list items with the slice operator by specifying the range of indices we want to extract. It uses the following syntax −

[start:stop] 

Where,

  • start is the starting index (inclusive).
  • stop is the ending index (exclusive).

If we does not provide any indices, the slice operator defaults to starting from index 0 and stopping at the last item in the list.

Example

In the following example, we are retrieving sublist from index 1 to last in “list1” and index 0 to 1 in “list2”, and retrieving all elements in “list3” −

list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]
list3 = ["Rohan", "Physics", 21, 69.75]

print ("Items from index 1 to last in list1: ", list1[1:])
print ("Items from index 0 to 1 in list2: ", list2[:2])
print ("Items from index 0 to index last in list3", list3[:])

Following is the output of the above code −

Items from index 1 to last in list1:  [''b'', ''c'', ''d'']
Items from index 0 to 1 in list2:  [25.5, True]
Items from index 0 to index last in list3 [''Rohan'', ''Physics'', 21, 69.75]

Access Sub List from a List

A sublist is a part of a list that consists of a consecutive sequence of elements from the original list. We can access a sublist from a list by using the slice operator with appropriate start and stop indices.

Example

In this example, we are fetching sublist from index “1 to 2” in “list1” and index “0 to 1” in “list2” using slice operator −

list1 = ["a", "b", "c", "d"]
list2 = [25.50, True, -55, 1+2j]

print ("Items from index 1 to 2 in list1: ", list1[1:3])
print ("Items from index 0 to 1 in list2: ", list2[0:2])

The output obtained is as follows −

Items from index 1 to 2 in list1: [''b'', ''c'']
Items from index 0 to 1 in list2: [25.5, True]
Translate »