FAQ: Working with Lists in Python - Slicing Lists

This community-built FAQ covers the “Slicing Lists” exercise from the lesson “Working with Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Slicing Lists

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

5 posts were split to a new topic: Slicing Lists

A post was merged into an existing topic: Slicing Lists - end index

Usually if lists are short, it would be easy to find out the indexes for the slicing method list[start:end] .
However, if lists happen to be extremely long, finding the indexes for the desired items becomes difficult. Is there a way to address this problem, like a more convenient method?

I don’t think there is one size fits all solution, it depends. You might use the index method to find the index. But this might be problematic if you have duplicate values.

Play Silly Games win Silly Prizes: Can’t believe that worked :rofl:


insert Mr. Robot Hacker Man.gif
Can I get my sweet hoodie now ???

What about :: colon mean?
I found these example in slice
print(tuple1[::-1])

that [::-1] means go through the entire list, and the -1 means do it backward

x = ['a', 'b', 'c', 'd', 'e', 'f'] # look at entire list, go backward y = x[::-1] print(y) # look at entire list, use every 2nd entry z = x[::2] print(z)
1 Like

Why doesn’t slicing work with negative numbers?

example:
num_list = [7, 6, 5, 4, 3, 2]
num_list[-1:-3]

I would expect the example to retrieve [2,3] but got an empty list. Is this because the list would be out of sequence when returned?

Consider the following example,

x = list(range(11))

print(x) 
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# From first element to end of list with step size of 1
print(x[:])
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# From last element to start of list with step of -1 (reverse direction)
print(x[::-1])
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# From element at index 4 to end of list
print(x[4:])
# [4, 5, 6, 7, 8, 9, 10]

# From element at index 2 (included) to element at index 9 (not included) 
# with step of 2
print(x[2:9:2])
# [2, 4, 6, 8]

# From element at index 4 to end of list with step of 3
print(x[4::3])
# [4, 7, 10]

# From first element to element at index -5 (not included) with step of 1
print(x[:-5])
# [0, 1, 2, 3, 4, 5]

# From element at index -5 (included) to end of list with step of 1
print(x[-5:])
# [6, 7, 8, 9, 10]

# From element at index -5 (included) to element at index 9 (not included)
# with step of 1
print(x[-5:9])
# [6, 7, 8]

# From element at index -9 (included) to element at index -4 (not included)
# with step of 1
print(x[-9:-4])
# [2, 3, 4, 5, 6]

# From element at index -3 (included) to element at index -1 (not included)
# with step of 1
print(x[-3:-1])
# [8, 9]

# From element at index 1 (included) to start of list 
# with step of -1 (reverse direction)
print(x[1::-1])
# [1, 0]

# From element at index -1 (included) to start of list
# with step of -1 (reverse direction)
print(x[-1::-1])
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

# From element at index -1 (included) to element at index -6 (not included)
# with step of -1 (reverse direction)
print(x[-1:-6:-1])
# [10, 9, 8, 7, 6]

# From element at index -3 (included) to element at index -5 (not included)
# with step of 1
print(x[-3:-5])
# []
# With a step of +1, it is not possible to start at index -3 and reach index -5

# From element at index 4 (included) to element at index 2 (not included)
# with step of 1
print(x[4:2])
# []
# With a step of +1, it is not possible to start at index 4 and reach index 2

With the above in mind, your example:

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

# From element at index -1 (included) to element at index -3 (not included)
# with step of 1
print(num_list[-1:-3])
# []
# With a step of +1, it is not possible to start at index -1 and reach index -3

# From element at index -1 (included) to element at index -3 (not included)
# with step of -1 (reverse direction)
print(num_list[-1:-3:-1])
# [2, 3]

You should create your own lists and play around with different combinations for upper and lower bounds and steps to further your understanding of slicing in Python.

Related Thread:

1 Like

Thanks for your response. I now see that slicing has more to offer than I realized.

1 Like