Course Content
Introduction to Python
List Methods
Let's consider the basic methods of manipulating a list.
len(t)
- returns the length of listt
(i.e., number of elements)list1 + list2
- concatenation (both terms must be lists)t * n
- returns n copies of listt
t.append(x)
- adds single itemx
at the end of listt
(this action rewrites the list)t.extend([x, y, ...])
- adds elementsx, y, ...
at the end of the listt
(rewrites the list, too)t.copy()
- returns a copy of the listt
t.count(x)
- returns the number of timesx
appears in listt
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