Use of slice() in Python

Sometimes the code becomes an unreadable mess of hardcoded slice indices. Consider the following code which stores the details of students(Name, Phone Number, Address, Marks in 5 subjects) in list, displays them one by one and finally displays the average total marks.

filter_none

edit
close

play_arrow

link
brightness_4
code

L = [('Student A', 9876543210, 'Adress of A', 99, 82, 97, 78, 69),
     ('Student B', 9999999999, 'Adress of B', 90, 83, 87, 72, 67),
     ('Student C', 8888888888, 'Adress of C', 88, 41, 56, 61, 93)]
  
  
# Display (name, address, phone no., avg marks) for each student
for student in L:
    print(student[0:3], "Marks:", sum(student[3:])/5)
  
# Average total marks of the class
sum_of_marks = 0
for student in L:
    sum_of_marks += sum(student[3:])
      
print(sum_of_marks / 3)

chevron_right


Output:

('Student A', 9876543210, 'Adress of A') Marks: 85.0
('Student B', 9999999999, 'Adress of B') Marks: 79.8
('Student C', 8888888888, 'Adress of C') Marks: 67.8
387.6666666666667

The above code does its work but a problem that might occur with this code is that if in future we decide to add an entry “Roll no” at index 1. Then the whole code will need to be changed. Also, this code is not so good at readability either. This is just a small example. In large projects we write thousands of lines of codes. Therefore, it becomes our duty to write readable and maintainable code.

The same code can be made readable and maintainable to an extent by naming the slices using the slice() constructor.

Syntax:



  • slice(stop)
  • slice(start, stop, step)

Parameters:
start: Starting index where the slicing of object starts.
stop: Ending index where the slicing of object stops.
step: It is an optional argument that determines the increment between each index for slicing.

Return Type: Returns a sliced object containing elements in the given range only.

Note: slice(1, 5, 2) corresponds to a[1:5:2] and slice(1, 4) corresponds to a[1:4].

Let’s rewrite our code.

filter_none

edit
close

play_arrow

link
brightness_4
code

l = [('Student A', 9876543210, 'Adress of A', 99, 82, 97, 78, 69),
     ('Student B', 9999999999, 'Adress of B', 90, 83, 87, 72, 67),
     ('Student C', 8888888888, 'Adress of C', 88, 41, 56, 61, 93)]
  
# Naming slice
details = slice(0, 3)
marks = slice(3, 8)
  
# display (name, address, phone no., avg marks) for each student
for student in l:
    print(student[details], "MArks:", sum(student[marks])/5)
  
# display average total marks of the class
sum_of_marks = 0
for student in l:
    sum_of_marks += sum(student[marks])
print(sum_of_marks / 3)

chevron_right


Output:

('Student A', 9876543210, 'Adress of A') MArks: 85.0
('Student B', 9999999999, 'Adress of B') MArks: 79.8
('Student C', 8888888888, 'Adress of C') MArks: 67.8
387.6666666666667

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.