educative.io

List slicing in python

num_list = [1, 2, 3, 4, 5, 6, 7, 8]

print(num_list[2:5])

print(num_list[0::2])

what is the double :: in [0::2] mean?


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @Matthew_McCane,

Thank you so much for reaching out to us. The slicing syntax is something like this list_name[starting_index::interval], so num_list[0::2] means that the list will start from the 0th index, and it will jump two indices and the output will be something like [1,3,5,7] and if we use num_list[1::3] we will get [2,5,8].

I hope I have answered your question but if there’s any question please feel free to let me know. Thanks.

1 Like

Greatly appreciate you taking your time to help me better understand that! EXCELLENT! I see now! Thank you Aswad.

1 Like