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 Basics (2015) Python Data Types Splitting and Joining

Sachin Kanchan
Sachin Kanchan
564 Points

LIST SLICING that I came across in Pycharm Edu tutorial. squares = [1, 4, 9, 16, 25] I need to print [4, 9, 16]

Can you use list/string indexing, and slicing to solve this. I am attaching ling to the image. You can find the instruction on the right-hand side, and the code at the center of the image.

https://www.dropbox.com/s/tmgxpaw7uboprqz/splicing.JPG?dl=0

1 Answer

David Bottrill
PLUS
David Bottrill
Courses Plus Student 10,910 Points

Hi Sachin, if you want to 'slice' a list you simply need to specify the start list element (with the first element starting at zero) and the last element (total number of elements plus one) with a : colon between the two index values. So to specify the middle three elements requires:

print(squares[1:4])