CodeBlessU
learn code with examples

Python Index, Slice, Access element

Index

All sequence types (list / tuple / array / string) supports indexing to access element. It is done by specifying index position in square brackets.

Specifying index is mandatory.

Syntax:
variable[index]
Return Value:
element
Python index starts from 0.
  • 0 refers to first element / front.
  • 1 refers to second element, so on...,
  • n-1 refers to nth element.

Index returns single element, while Slice returns sequence (list / tuple / array / string).


Negative Index, Backward Index

Negative index denoted by number -1 to -n.
  • -1 refers to last element.
  • -2 refers to second from last, so on...,
  • -n refers to first element.

IndexError

If index is indicating position that does not exists, i.e. outside its range, it raises IndexError while accessing element. It does not raise error in Slice.

  • Forward index range is 0 to n-1.
  • Backward index range is -1 to -n.

Slice

Slice is done by specifying start index and end index in square brackets. It will return sequence (i.e. list slice will return list, array slice will return array). It might be empty sequence or sequence with one element or sequence with multiple / all elements. Start index element is part of return sequence. End index element is not part of return sequence.

Specifying start index, end index are optional. If slice start index is not specified, it starts from first element (inclusive). If slice end index is not specified, it ends after last element.

If start index is higher than end index, it will return empty sequence.

Start index and/or end index can be positive or negative.

Start index and/or end index can be out of range. It does not raise IndexError in slice.

Slice output may not have same object id, even if slice returns complete sequence. For list / array, it showed different id, but tuple showed same id.

Syntax:
variable[start:end]
Return Value:
sequence

List index

# Example, list index
a1 = [ 100, 200, 10, 20, 0, 500]

# Example, access first element in list
print(a1[0]) # 100

# fourth element
print(a1[3]) # 20

Note:
  • a1 = [0], creates list. (Square brackets on right side).
  • a1[0], accesses element. (Square brackets with index).

List negative index

# Example, list negative index
a1 = [ 100, 200, 10, 20, 0, 500]

# Example Backward access, last element in list
print(a1[-1]) # 500

# fourth from last
print(a1[-4]) # 10

IndexError: list index out of range

Why this error:

Did you specified index value outside its range?

Solution:

Forward index range is 0 to n-1. Backward index range is -1 to -n.

# Example, list IndexError
a1 = [ 100, 200, 10, 20, 0, 500]

# Example, access list outside range
print(a1[6]) # raises IndexError. Invalid: 6 onwards, Valid: 0 to 5.
print(a1[-7]) # raises IndexError. Invalid: -7 and lower, Valid: -1 to -6.

List Slice

# Example, list slice
a1 = [ 100, 200, 10, 20, 0, 500]

a1[0:3] # [100, 200, 10]
a1[0:1] # [100]

List slice with start index and end index is same

# Example, list slice with start index and end index is same
a1 = [ 100, 200, 10, 20, 0, 500]

a1[0:0] # []

List slice with start index / end index not specified

# Example, list slice with start index / end index not specified
a1 = [ 100, 200, 10, 20, 0, 500]

a1[:2] # [100, 200]
a1[3:] # [20, 0, 500]
a1[:] # [100, 200, 10, 20, 0, 500]
a1[] # raises SyntaxError: invalid syntax

List slice with start index higher than end index

# Example, list slice with start index higher than end index
a1 = [ 100, 200, 10, 20, 0, 500]

a1[3:2] # []

List slice with start index / end index is negative

# Example, list slice with start index / end index is negative
a1 = [ 100, 200, 10, 20, 0, 500]

a1[:-2] # [100, 200, 10, 20]
a1[-2:] # [0, 500]

a1[-2:-4] # [], Start index is higher than end index!
a1[-4:-2] # [10, 20]

List slice with mix (positive and negative) index

# Example, list slice with mix (positive and negative) index
a1 = [ 100, 200, 10, 20, 0, 500]

a1[1:-2] # [200, 10, 20]
a1[-4:3] # [10]

List slice with start index / end index out of range

# Example, list slice with start index / end index out of range
a1 = [ 100, 200, 10, 20, 0, 500]

a1[3:10] # [20, 0, 500]
a1[-10:2] # [100, 200]
a1[-10:10] # [100, 200, 10, 20, 0, 500]

List slice and source list are different object

# Example, list slice and source list are different object
a1 = [ 100, 200, 10, 20, 0, 500]

# Observe, different id
id( a1[0:6] )
id( a1[:] )
id(a1)

# Observe, different id but same output
a1[0:6]
a1[:]
a1

Tuple index

# Example, Tuple index
a1 = ( 100, 200, 10, 20, 0, 500 )

# Example, access first element in Tuple
print(a1[0]) # 100

# fourth element
print(a1[3]) # 20

Tuple negative index

# Example, Tuple negative index
a1 = ( 100, 200, 10, 20, 0, 500 )

# Example Backward access, last element in Tuple
print(a1[-1]) # 500

# fourth from last
print(a1[-4]) # 10

IndexError: Tuple index out of range

Why this error:

Did you specified index value outside its range?

Solution:

Forward index range is 0 to n-1. Backward index range is -1 to -n.

# Example, Tuple IndexError
a1 = ( 100, 200, 10, 20, 0, 500 )

# Example, access Tuple outside range
print(a1[6]) # raises IndexError. Invalid: 6 onwards, Valid: 0 to 5.
print(a1[-7]) # raises IndexError. Invalid: -7 and lower, Valid: -1 to -6.

Tuple Slice

# Example, Tuple slice
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[0:3] # (100, 200, 10)
a1[0:1] # (100,)

Tuple slice with start index and end index is same

# Example, Tuple slice with start index and end index is same
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[0:0] # ()

Tuple slice with start index / end index not specified

# Example, Tuple slice with start index / end index not specified
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[:2] # (100, 200)
a1[3:] # (20, 0, 500)
a1[:] # (100, 200, 10, 20, 0, 500)
a1[] # raises SyntaxError: invalid syntax

Tuple slice with start index higher than end index

# Example, Tuple slice with start index higher than end index
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[3:2] # ()

Tuple slice with start index / end index is negative

# Example, Tuple slice with start index / end index is negative
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[:-2] # (100, 200, 10, 20)
a1[-2:] # (0, 500)

a1[-2:-4] # (), Start index is higher than end index!
a1[-4:-2] # (10, 20)

Tuple slice with mix (positive and negative) index

# Example, Tuple slice with mix (positive and negative) index
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[1:-2] # (200, 10, 20)
a1[-4:3] # (10,)

Tuple slice with start index / end index out of range

# Example, Tuple slice with start index / end index out of range
a1 = ( 100, 200, 10, 20, 0, 500 )

a1[3:10] # (20, 0, 500)
a1[-10:2] # (100, 200)
a1[-10:10] # (100, 200, 10, 20, 0, 500)

id check for Tuple slice and source Tuple

# Example, Tuple slice and source Tuple are different object
a1 = ( 100, 200, 10, 20, 0, 500 )

# Observe, different id
id( a1[0:6] )
id( a1[:] )
id(a1)

# Observe, different id but same output
a1[0:6]
a1[:]
a1

Array index

# Example, Array index
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

# Example, access first element in Array
print(a1[0]) # 100

# fourth element
print(a1[3]) # 20

Array negative index

# Example, Array negative index
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

# Example Backward access, last element in Array
print(a1[-1]) # 500

# fourth from last
print(a1[-4]) # 10

IndexError: array index out of range

Why this error:

Did you specified index value outside its range?

Solution:

Forward index range is 0 to n-1. Backward index range is -1 to -n.

# Example, Array IndexError
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

# Example, access Array outside range
print(a1[6]) # raises IndexError. Invalid: 6 onwards, Valid: 0 to 5.
print(a1[-7]) # raises IndexError. Invalid: -7 and lower, Valid: -1 to -6.

Array Slice

# Example, Array slice
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[0:3] # array('i', [100, 200, 10])
a1[0:1] # array('i', [100])

Array slice with start index and end index is same

# Example, Array slice with start index and end index is same
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[0:0] # array('i') - its empty array

Array slice with start index / end index not specified

# Example, Array slice with start index / end index not specified
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[:2] # array('i', [100, 200])
a1[3:] # array('i', [20, 0, 500])
a1[:] # array('i', [100, 200, 10, 20, 0, 500])
a1[] # raises SyntaxError: invalid syntax

Array slice with start index higher than end index

# Example, Array slice with start index higher than end index
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[3:2] # array('i')

Array slice with start index / end index is negative

# Example, Array slice with start index / end index is negative
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[:-2] # array('i', [100, 200, 10, 20])
a1[-2:] # array('i', [0, 500])

a1[-2:-4] # array('i'), Start index is higher than end index!
a1[-4:-2] # array('i', [10, 20])

Array slice with mix (positive and negative) index

# Example, Array slice with mix (positive and negative) index
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[1:-2] # array('i', [200, 10, 20])
a1[-4:3] # array('i', [10])

Array slice with start index / end index out of range

# Example, Array slice with start index / end index out of range
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

a1[3:10] # array('i', [20, 0, 500])
a1[-10:2] # array('i', [100, 200])
a1[-10:10] # array('i', [100, 200, 10, 20, 0, 500])

Array slice and source Array are different object

# Example, Array slice and source Array are different object
import array
a1 = array.array('i', (100, 200, 10, 20, 0, 500) )

# Observe, different id
id( a1[0:6] )
id( a1[:] )
id(a1)

# Observe, different id but same output
a1[0:6]
a1[:]
a1

In this page...

Copyright © All rights reserved.