Foundations of Python

98 / 134

List Methods

Just like strings, you can check for methods for lists as well using dir and help. Some important methods are like append, extend and sort.

  • append adds a new element to the list,
  • extend takes a list as an argument and appends all of the elements,
  • sort arranges the elements of the list from small to large.

Understand the code and make use of notebook to figure out the results,

a = [23,12,32,1,2,34,56]
a.append(12)
print(a[len(a)-2])

Enter the number printed

a.extend(a)

Enter the sum of all elements within a

a.sort()
print(a[2] + a[7])

Enter the number printed

Some other functions are,

  • sum() only works when the list elements are numbers to calculate the sum of all elements
  • other functions (max(), len(), etc.) work with lists of strings and other types that can be comparable.
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...