How to Slice Lists/Arrays and Tuples in Python

Posted in /  

How to Slice Lists/Arrays and Tuples in Python
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    In Python, slicing represents accessing a sequence of elements and characters from string, tuple, and List. Slicing is one of the best techniques in Python to get different substrings of a Python string . To know how to use slicing in Python List, tuple, and string, you should know about what is Indexing in Python and how can we use the index to access individual values and character from a string, tuple, and List.

    What is indexing in Python list, tuple, and string?

    Python list , tuple, and string are ordered Python data structure and they store their elements in sequential order, and every element or character present in these data structures has a specific index value and with the help of the element or character index value, we can grab or access the individual element from these data structures. The initialization or declaration of all Python list, Python Tuple , and Python string is different. Python string is represented as a sequence of characters inside a single or double quote.

    >>> string = "This is a String"
    >>> print(string)
    This is a String

    Python list is represented as a sequence of elements separated by commas inside a square bracket.

    >>> my_list = [1, "two", 3.0]
    >>> print(my_list)
    [1, 'two', 3.0]

    Python tuple represented as a sequence of elements separated by a comma inside the parenthesis.

    >>> my_tuple = (1, 'two', 3.0)
    >>> print(my_tuple)
    (1, 'two', 3.0)

    Although the representation of these data types is different, but the syntax for accessing elements and characters is similar. The index value for these three data structures starts from 0 and goes up to n-1 where n is the total number of elements or characters present in the data structure.

    >>> my_list =[1, 'two', 3.0]
    >>> my_list[0]  # accessing my_list first element
    1
    
    >>> my_tuple=(1, 'two', 3.0)
    >>> my_tuple[1]  # accessing my_tuple second element
    'two'
    
    >>> string = "This is a String"
    >>> string[2]  #accessing string 3rd character
    'i'

    Negative indexing in Python list, tuple, and string.

    Python list, tuple, and string also support negative indexing, which helps us to access the list, tuple, and string values from the back. The positive indexing starts from 0 to n-1 that access the data values from left to right or front to end. The negative indexing starts from -1 to -n that access the data values from right to left or end to front.

    >>> string = "This is a String"
    >>> string[-1]  #accessing string last character
    'g'
    
    >>> my_tuple=(1, 'two', 3.0)
    >>> my_tuple[-2]  # accessing my_tuple second last element
    'two'

    Slicing in Python list/tuple and string

    By far we have learned that how can we use indexing to grab or access the individual element or character from a Python list, Python tuple, and Python string. But using the slicing we can grab a sub-string, sub-list, and sub-tuple from Python string, list, and tuple. Like Python indexing, Python slicing also uses the data type index value.

    Python list, tuple, and string slicing syntax

    var_name[start_index: end_index :  step] The slicing is divided into three part using the colon symbol The start_index represents the starting index value, from where the slicing should be started. The end_index represents the end index value, up to where the slicing will be ended, and the end index value is excluded. The step represents the increment between each index for slicing.

    >>> my_list  = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list[ 2: 6: 2]
    [30, 50]

    In the above example, the starting index value is 2 and the end index value is 6 , and slicing performed using 2 steps each.

    How to do slicing In Python list/tuple and String

    Now let’s discuss the different cases we can perform with slicing.

    Python list/tuple slicing using start and end index

    In Slicing the step value is optional without it the slicing can also be performed. If we do not mention the step, then by default its value becomes 1, and indexing increase with 1 step. [start_index : end_index] When we specify the start and end index, the element of the start index is included but not the end one.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list[2: 7 ]
    [30, 40, 50, 60, 70]
    
    >>> string = "This is a string "
    >>> string[1: 8]
    'his is '
    
    >>> my_tuple = (100, 200, 300, 400, 500, 600, 700, 800)
    >>> my_tuple[0:6]
    (100, 200, 300, 400, 500, 600)

    In the above example, you can see that the staring index value is included but not the end index. In my_tuple[0:6] only my_tuple[0] to my_tuple[5] elements were sliced.

    Python list/tuple slicing without start and end index [ : ]

    If we do not specify either the start or end index value, by default the start index becomes 0, and the end index becomes n where n is the total number of elements or character present in the list, tuple, and string.

    [ : ] >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80] >>> my_list[ : 4] [10, 20, 30, 40] >>> my_tuple = (100, 200, 300, 400, 500, 600, 700, 800) >>> my_tuple[4:] (500, 600, 700, 800) >>> string = "This is a string " >>> string[ : ] 'This is a string '

    Python list/tuple slicing out of range index value

    Unlike indexing in slicing no error occur if either start or end values are out of the index. If the starting index value is out of index then we get an empty list, tuple, or string. If the ending index value is out of index then we get a list, tuple, or string slicing from the starting point to the end.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list[ 100: 5]
    []
    
    >>> my_tuple = (100, 200, 300, 400, 500, 600, 700, 800)
    >>> my_tuple[5: 100]
    (600, 700, 800)

    Python list/tuple slicing with start index, end index, and step [ start : end : step]

    The step represents the increment or decrement count of the index slicing. By default, its value is 1 which signifies 1 increment of index value within slicing. But we can change it to any integer value, for positive indexing in which we are accessing the list, tuple or string from left to right there we use the positive step value. In slicing where we are accessing the list, tuple, and string from right to left or back to front there we use the negative step value. If we specify the step value to 2 then it will increment every index value by two and select every second value from the starting point to the end. This similar process will go with if we specify the step to 3, 4, 5, and so on. < Note >: The step value could be any number except 0.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list[ : 8: 2 ]
    [10, 30, 50, 70]
    
    >>> my_tuple = (100, 200, 300, 400, 500, 600, 700, 800)
    >>> my_tuple[  :  : 3  ]  #select every 3rd element from 0 to n index value.
    (100, 400, 700)

    Python list/tuple Negative Slicing

    Similar to the negative indexing we can perform negative slicing to access list, tuple, and string elements from back to forth. When we do negative slicing with Python list, string or tuple we need to specify the step value, else we get an empty list or string. Because by default the step value is 1 and in negative slicing, we do not want to increment the index value with 1, we want to decrement the value by any negative number.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list [-1 : -6]
    []

    The above example returns an empty list because we did not specify the step value.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list [-1:-6:-1]
    [80, 70, 60, 50, 40]
    
    >>> my_list[ 2:-1]  #-1 represent the last element which is equal to n-1
    [30, 40, 50, 60, 70]

    Assigning value by slicing

    Using slicing we can also assign value to a list, slicing assigning is only applied to a Python list because string and tuples are immutable. In slicing assigning, the number of the selected slicing has to do nothing with the number of items to be assigned.

    >>> my_list = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> my_list[2:5] =[300, 400, 500]
    
    >>> my_list
    [10, 20, 300, 400, 500, 60, 70, 80]
    
    >>> my_list[2:5] =[0]
    
    >>> my_list
    [10, 20, 0, 60, 70, 80]

    Conclusion

    In this article, we learned what is slicing in Python and how to slice lists and tuples in Python. To perform the slicing, we mention the start and end index value separated with a colon. By default, the start value is 0, the end value is n, and step value is 1. The step value in indexing specifies the increment value for every next selected index. It is optional if we are performing the slicing from front to back, but if we are accessing elements from back to front then we need to specify the step value to any negative integer.

    People are also reading:

    Leave a Comment on this Post

    0 Comments