Previous

Iteration

2 / 5
Next

Lists are special Python objects used to store multiple data entries in one variable.

fruits = ['apple', 'banana', 'coconut']

We can access and modify list elements using index notation with square brackets [ and ]. Indexing starts at 0.

print(fruits[1]) # banana
fruits[2] = 'cherry'
print(fruits) # ['apple', 'banana', 'cherry']

List objects have many methods, such as append, to add elements to the end of the list:

fruits.append('fig')
print (fruits) # output: ['apple', 'banana', 'coconut', 'fig']

Here are some other methods that can be used to modify Python lists in place:

Method Description
append(x) Add an item to the end of the list
clear() Remove all items from the list
copy() Return a shallow copy of the list
count(x) Return the number of occurrences of x
index(x[,start[,end]]) Return first index of x
insert(i, x) Insert x at position i
pop([i]) Remove and return item at index i (default last)
remove(x) Remove first occurrence of x
reverse() Reverse the list in place
Send

Lists

Hint
Advanced

Write two functions shift_left and shift_right:

  • Both functions have one parameter arr.
  • shift_right moves the last element of arr to the first position.
  • shift_left moves the first element of arr to the last position.
Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!