Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-slice-list-by-value

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: python-slice-list-by-value
Showing posts with label python-slice-list-by-value. Show all posts
Showing posts with label python-slice-list-by-value. Show all posts

Python Slicing a list

Python Slicing a list





Slicing a list

You can work with any set of elements from a list. A portion of a list is called a slice. To slice a list start with the index of the first item you want, then add a colon and the index after the last item you want. Leave off the first index to start at the beginning of the list, and aleave off the last index to slice through the end of the list.

#Getting the first three items 
finishers = ['kai', 'abe', 'ada', 'gus', 'zoe']
first_three = finishers[:3]
#Getting the middle three items
middle_three = finishers[1:4]
#Getting the last three items
last_three = finishers[-3:]

Screenshot