Take parts of a list with [start:stop:step], reverse it, and copy it so changes do not leak back.
Python · beginner · 6 min · 8 steps · free
days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
weekdays = days[:5] # from the start up to index 5
weekend = days[5:] # from index 5 to the end
every_other = days[::2] # step 2
backwards = days[::-1] # step -1 reverses
copy = days[:] # a new list with the same items
copy[0] = "MON"
print(weekdays, weekend, every_other, backwards, days[0], copy[0])days is created and points to a new list: ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'].[:] slices everything: a new list with the same items.b = a copies a list: it only adds a second nameAnswer: [2, 3]. From index 1 up to, not including, 3.
Answer: [3, 2, 1]. A step of -1 reverses.
← Lists: Adding, Removing and Sorting · Two Names, One List →