How to Extract Segments with Python List Slice?

Python List Slice lets you to extract sections of interest from a list. Slicing is done by specifying the start and end index with in square parenthesis with the name of the list name. The start and end index are separated by colon ‘:’.  Python List Slice returns a Python List.

Syntax for Python List Slice

List_Name[ Start_Index: End_Index]

List_Name- Name of the list that has already been declared.

Start_Index – it’s the index from where you want to extract the list slice.

End_Index- It’s the index of the element before which you need to extract the elements. If you want to extract till 4th element then the End_Index should be specified as 5.

Important points for slicing

Consider this example of a python list

myShoppingList=[‘apples’,12,’Grapes’,’toothpaste’,1,’butter’,’shampoo’,’milk’]

Its indices from left to right or right to left are displayed in the image below

Python List Slice
Python List Slice
  • Indexes in a Python list are
    • 0 to count-of-elements-1 ( if count of elements is 8  indexes are 0 to 7)
    • –count-of-elements to  -1 ( if count of elements is 10  indexes are -8 to -1)
  • The indexes can be null. If Start_Index is Null it represents 0. If End_index is Null it represents index of last element.
  • The Start_index must always be greater in value than the End_index.
  • Both indexes can be empty, negative, positive or zero.
  • A negative index refers to element’s position from the end. -1 represents the last element.
  • If Start_Index is positive then End_index can be positive or negative. If positive slice will be created from Start_Index to End_index -1
  • If Start_Index is negative then End_index can be positive, negative or zero.
    •  If End_index  is positive, slice will be created from Start_Index to End_index.
    •  If  End_index is zero, slice will be created from the negative start index to the first element.
    • If  End_index is negative, it must be lesser than the start_index.  Slice will be created from the negative start index to the element before the negative End_Index element.

Examples of Python List Slice

Undefined or Null Indexes

myShoppingList[:] // displays list of all elements

Output : [‘apples’, 12, ‘Grapes’, ‘toothpaste’, 1, ‘butter’, ‘shampoo’, ‘milk’]

myShoppingList[:5] // displays list from 0 to 4 index

Output: [‘apples’, 12, ‘Grapes’, ‘toothpaste’, 1]

myShoppingList[5:] // displays list from 5 to 7 index

Output: [‘butter’, ‘shampoo’, ‘milk’]

myShoppingList[-5:] :] // displays list from -5 to 7 index

Output: [‘toothpaste’, 1, ‘butter’, ‘shampoo’, ‘milk’]

myShoppingList[:-3] :] // displays list from 0 to -4 index

Output: [‘apples’, 12, ‘Grapes’, ‘toothpaste’, 1]

Positive Start_Index and End_Index

myShoppingList[3:6] // displays elements from index 3 to index 5

Output: [‘toothpaste’, 1, ‘butter’]

Positive Start_Index and negative End_Index

myShoppingList[3:-3] // displays elements from index 3 to  fourth element from end (-3-1)

Output: [‘toothpaste’, 1]

myShoppingList[3:-6]// returns empty list since End_Index is less than Start_Index

Output: []

myShoppingList[2:-5]// returns single element list from index 2 to index -6(-5-1)

Output: [‘Grapes’]

myShoppingList[:-5]// since Start_Index is not assigned it is taken as 0. This returns elements from 0 index to 6th element from end or 3rd element from start

Output: [‘apples’, 12, ‘Grapes’]

Negative Start_Index and negative End_Index

myShoppingList[-1:-5]// Return Empty list as Start_Index is less than End_index

Output: []

myShoppingList[-5:-1] ]// Return list  from end with Start_Index -5 to End_index -2

Output: [‘toothpaste’, 1, ‘butter’, ‘shampoo’]

Negative Start_Index and positive End_Index

myShoppingList[-7:6] ]// Return list  from end with Start_Index -7 to End_index 5

Output: [12, ‘Grapes’, ‘toothpaste’, 1, ‘butter’]

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *