Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python List Slicing
# Python list can be manipulated as follows:
my_list = [1,2,3,4,5,6,7,8,9,10]
# my_list[start_index : end_index : stride]
# Default for start index is 0
# Default for end index is the end of the list
even_num_list = my_list[1::2]
# Returns
[2, 4, 6, 8, 10]
odd_num_list = my_list[::2]
# Returns
[1, 3, 5, 7, 9]
reverse_list = my_list[::-1]
# Returns
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment