course content

Course Content

Introduction to Python

List MethodsList Methods

Let's consider the basic methods of manipulating a list.

  • len(t) - returns the length of list t (i.e., number of elements)
  • list1 + list2 - concatenation (both terms must be lists)
  • t * n - returns n copies of list t
  • t.append(x) - adds single item x at the end of list t (this action rewrites the list)
  • t.extend([x, y, ...]) - adds elements x, y, ... at the end of the list t (rewrites the list, too)
  • t.copy() - returns a copy of the list t
  • t.count(x) - returns the number of times x appears in list t

For example, we'll update the list from the previous chapter with new data (the capital and the number of states).

Please note that the .extend() method requires an iterable object as the argument. Here we'll use lists as iterable objects.

Section 4.

Chapter 2