Arrays & Lists
#
Note: This page is a work-in-progressIn this chapter, we are going to look at a data structure you will be using extensively - arrays (called lists in Python).
Lists can comprise any data types (e.g. int
, string
or even other List
types). Lists are similar to strings in the way that they are accessed by the index.
In this example, we are accessing index 2
of the list. Since the index starts from 0
, at index 2
, we have the element 3.
Similarly, we can iterate over lists using the for
loop:
#
Useful Methods for Lists in PythonThe below table has a few useful examples of methods we can use in Python lists. a
refers to a list in the below methods.
Method | Description |
---|---|
a.append(x) | Appends x to the end of the list |
a.count(x) | Counts how many times x appears in the list |
a.remove(x) | Remove x from list |
a.reverse() | Reverse the order of the list |
a.sort() a.sort(reverse=True) | Sort the list in ascending order, add reverse=True for descending order |