Course Outline (Part 11)

Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data.

A list is a collection which is ordered, changeable (mutable), and allows duplicate members.


1. Create list

Lists are created using square brackets [].

thislist = ["apple", "banana", "cherry"]
print(thislist)

List items can be of any data type, and a single list can contain different data types:

list1 = ["abc", 34, True, 40, "male"]

2. Access items (indexing)

List items are indexed and you can access them by referring to the index number. Remember, the first item has index 0.

thislist = ["apple", "banana", "cherry"]
print(thislist[1]) # Outputs "banana"

Negative Indexing

Negative indexing means start from the end. -1 refers to the last item, -2 refers to the second last item etc.

print(thislist[-1]) # Outputs "cherry"

3. Change list items

To change the value of a specific item, refer to the index number.

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist) # ['apple', 'blackcurrant', 'cherry']

4. Add items

  • append(): To add an item to the end of the list.
  • insert(): To insert a list item at a specified index.
  • extend(): To append elements from another list (or any iterable) to the current list.
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist) # ['apple', 'banana', 'cherry', 'orange']

thislist.insert(1, "mango")
print(thislist) # ['apple', 'mango', 'banana', 'cherry', 'orange']

tropical = ["pineapple", "papaya"]
thislist.extend(tropical)

5. Remove items

  • remove(): Removes the specified item (the first occurrence).
  • pop(): Removes the specified index. If no index is specified, it removes the last item.
  • del: Removes the specified index, or deletes the entire list.
  • clear(): Empties the list. The list still remains, but it has no content.
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")

thislist.pop(0) # Removes "apple"

del thislist[0] # Removes the remaining "cherry"

thislist.clear() # Empties the list

6. Loop through list

You can loop through the list items by using a for loop.

thislist = ["apple", "banana", "cherry"]
for x in thislist:
    print(x)

7. List comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Syntax: newlist = [expression for item in iterable if condition == True]

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]

print(newlist) # ['apple', 'banana', 'mango']

8. Sort lists

List objects have a sort() method that will sort the list alphanumerically, ascending, by default.

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

# Sort descending
thislist.sort(reverse = True)

Note: sort() modifies the original list. The sorted() function returns a new sorted list while leaving the original intact.


9. Copy lists

You cannot copy a list simply by typing list2 = list1, because list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.

Use the copy() method or the list() constructor to make a true copy:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
# or
mylist = list(thislist)

10. Join lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways is by using the + operator.

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

Alternatively, you can use the extend() method to add list2 into list1.

Discussion

Loading comments...