Tutorial by Examples

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] lst[::2] # Output: ['a', 'c', 'e', 'g'] lst[::3] # Output: ['a', 'd', 'g']
lst = ['a', 'b', 'c', 'd', 'e'] lst[2:4] # Output: ['c', 'd'] lst[2:] # Output: ['c', 'd', 'e'] lst[:4] # Output: ['a', 'b', 'c', 'd']
a = [1, 2, 3, 4, 5] # steps through the list backwards (step=-1) b = a[::-1] # built-in list method to reverse 'a' a.reverse() if a = b: print(True) print(b) # Output: # True # [5, 4, 3, 2, 1]
def shift_list(array, s): """Shifts the elements of a list to the left or right. Args: array - the list to shift s - the amount to shift the list ('+': right-shift, '-': left-shift) Returns: shifted_array - the shifted list "&quo...

Page 1 of 1