List Basics#

Note:

You can experiment with this code — and you should — by going here:

Binder

Strings and lists have a great many things in common:

strings and lists

length: len()

use of in

ordered

indexed

slice-able

iterate over

List Creation#

A list can be created as a container of comma-separated values (usually called “items”) between square brackets. As said previously, lists might contain items of different types but often the items all have the same type. (That is really up to you and your use case.)

# square brackets with comma-separated items
grocery_list = ['whole milk', 'eggs', 'steak'] 
family_members = ['Fred', 'Alice', 'Ted', 'Abundantia', 'George', 'Betty', 'Aloysius']

# integer list
squares = [1, 4, 9, 16, 25]
square_floats = [1.0, 4.0, 9.0, 25.0]
squares
[1, 4, 9, 16, 25]
# combined data types

bugs_info = ['Bugs', 87, True]

# nested list -- list of lists
more_data = [[3,4,5], [7,8,9]]

more_data
[[3, 4, 5], [7, 8, 9]]

Reading Items from a List: Indexing and Slicing#

Lists, being a sequential (or ordered) collection of items, have an index to them just as with strings.

# extracting/reading the data in a list
# using indexing
name = bugs_info[0]
age = bugs_info[1]
hare = bugs_info[2]

print(name, age, hare)
Bugs 87 True

And, just as with strings, lists can be sliced:

# slicing from a list
older_family_members = family_members[0:4]
older_family_members
['Fred', 'Alice', 'Ted', 'Abundantia']

Changing/Updating Items in a List#

Unlike strings, lists can be altered, i.e., they are mutable. Thus:

# Bugs has a birthday
bugs_info[1] = 88
bugs_info
['Bugs', 88, True]

Adding Items to a List#

As with strings, lists come with a host of methods, i.e., functions / processes that make your life easier. One of these is append(), which allows you to easily add an item to the end of the list:

family_members.append('Ulysses')
# now Ulysses has joined the clan
family_members
['Fred',
 'Alice',
 'Ted',
 'Abundantia',
 'George',
 'Betty',
 'Aloysius',
 'Ulysses']

Moreover, you can use the list insert() method, in the form list.insert(index, new_item)

That is, you insert an item at a given index position.

The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list.

Whereas a.insert(len(a), x) is equivalent to a.append(x).

# new patriarch in family
family_members.insert(0, 'Guido')
family_members
['Guido',
 'Fred',
 'Alice',
 'Ted',
 'Abundantia',
 'George',
 'Betty',
 'Aloysius',
 'Ulysses']

Deleting or Removing an Item from a List#

Python offers a number of methods in order to remove or delete an item from a list. But they are distinguishable primarily by how they are used.

See Python docs

  • list.remove(x)

Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.

  • list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

  • list.clear()

Remove all items from the list.

# remove

The del statement#

The del statement removes an item from a list given its index position instead of its value. That is, it differs from the pop() method which returns a value.

The del statement can also be used to remove slices from a list or clear the entire list:

a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
a
[1, 66.25, 333, 333, 1234.5]
del a[2:4]
a
[1, 66.25, 1234.5]
del a[:]
print(a)
# now a is empty, but still around
[]

Calling del a obliterates a. If you attempt to use a, you will throw a NameError: name 'a' is not defined