Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Slices First Slice

Please fill in the correct answer in each blank provided below.

i am struggling in getting the right answer in this quiz

5 Answers

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi in python if you wish to slice the whole list you can achieve this just like behar says, start at index 0 and finish at the end.

a_list = [1, 2, 3]
a_list[:]
>>> [1, 2, 3]

The correct answer is 0. The index method works like this = index[starting point:ending point:steps], seeing as its empty at the starting point, its 0.. Hope this helps!

o, so this is how slices work...

array_name[start:end:iterable]

if:

  • start not provided, 0 is considered as default.
  • end not mentioned, it goes till last of the array.
  • iterable not given, it iterates one by one. i.i default value is 1.

Example:

arr1 = [1, 2, 3, 4, 5]
arr1[:]
#will give the whole array.
#notice the iterable is optional 
arr[::2]
# will give... [1,3,5]
arr[1:3:2]
# will give... [2]

hope this helps.

Scott Junner
Scott Junner
9,010 Points

I'll help you with the problem in a moment. First we have to deal with something more important that will have a profound impact on peoples willingness to help you. I had to be told. At some point we all get told.

You have to help people help you. The subject line of your question is no help to anyone. No one can see what the question is really about. So no one knows if they can help you. Your query is about list slicing in python. So it would help everyone if you made that the subject of your query. And it would also help people searching the forums in the future.

Now on to the help. The question is asking you to use list slicing syntax to select the whole list. it gives you...

a_list = [1, 2, 3]

Let's say we want to get the first two items in the list above using list slicing. To do that we would write

a_list[0:2]

which would return the list slice [1, 2]. Also the list slice

a_list[:2]

will return exactly the same. The sublist, or list slice [1, 2].

Now if we wanted the last two elements the two following list slices would also produce the same results.

a_list[1:3]    //or you can use
a_list[1:]

Both return the sub list, or list slice [2, 3] So now see if you can figure out how to get the whole list using list slicing.

The best thing to do is just open up a python terminal and play about with the syntax and see if you can break it. Break it as many times as you can. Make as many mistakes and errors as you can. It really helps.

mdmaleksharafali
mdmaleksharafali
2,392 Points

The <aside> element contains content that's indirectly related to the main content of the page.