0
0
Pythonprogramming~5 mins

List indexing and slicing in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is list indexing in Python?
List indexing means accessing an element in a list by its position number. Positions start at 0 for the first item.
Click to reveal answer
beginner
How do you access the last item of a list using indexing?
Use index -1 to get the last item of a list. For example, my_list[-1] gives the last element.
Click to reveal answer
beginner
What does list slicing do?
List slicing extracts a part of the list by specifying a start and end position. It creates a new list with those elements.
Click to reveal answer
intermediate
Explain the syntax my_list[start:end] in slicing.
It means take elements from position start up to but not including end. If start is omitted, it starts from the beginning. If end is omitted, it goes to the end.
Click to reveal answer
intermediate
How can you get every second element from a list using slicing?
Use the step value in slicing: my_list[::2] returns every second element starting from the first.
Click to reveal answer
What is the index of the first element in a Python list?
A1
B0
C-1
DNone
What does my_list[-2] return?
AThe second element
BThe last element
CThe second last element
DAn error
What will my_list[2:5] return?
AElements from index 2 to 4
BElements from index 0 to 5
CElements from index 3 to 5
DElements from index 2 to 5 inclusive
How do you get a copy of the whole list using slicing?
Amy_list[::0]
Bmy_list[0]
Cmy_list[-1]
Dmy_list[:]
What does my_list[1:6:2] do?
AGets elements from 1 to 6 stepping by 2
BGets every element from 1 to 6
CGets elements 1 and 6 only
DReturns an error
Describe how to access elements in a list using positive and negative indexes.
Think about counting from the start and from the end.
You got /3 concepts.
    Explain how list slicing works and how to use start, end, and step values.
    Imagine cutting a piece from a list with rules.
    You got /4 concepts.