Search
Lists

Lists

So far you've learned about and used a few data types: integers, floats, booleans, and strings. In this section you'll learn about a new data type with which you can store a collection of other data types. We call this a list. A list is a collection of objects, similar to an array if you've programmed in other languages that use those. You define a list in Python by enclosing a comma-separated sequence of objects in square brackets ([]), as shown below:

ciphers = ['keyword', 'caesar', 'multiplicative', 'affine']
print(ciphers)
['keyword', 'caesar', 'multiplicative', 'affine']

There are several helpful features about lists that make them very versatile:

  • Lists are ordered.
  • Lists can contain any type of objects, even a mix of them, including other lists.
  • List elements can be accessed by index, just like characters in a string.

Lists Are Ordered

When you add items to a list, they are stored in the same order in which they were added. If you were to compare two lists with the same elements and ask Python if they were the same, it would depend on if those elements were in the same order

print( [1,2,3] == [1,2,3] )
print( [3,2,1] == [1,2,3] )
True
False

Lists Can Contain Any Type of Objects

Lists can hold only one type of data:

ciphers = ['keyword', 'caesar', 'multiplicative', 'affine']
print( ciphers )
['keyword', 'caesar', 'multiplicative', 'affine']

A mix of different types of data:

quatro = ['four', 4, 4.0]
print( quatro )
['four', 4, 4.0]

And if you want, even other lists:

substitution = ['keyword', 'caesar', 'multiplicative', 'affine']
transposition = ['route', 'railfence']

allciphers = [transposition , substitution]

print( allciphers )
[['route', 'railfence'], ['keyword', 'caesar', 'multiplicative', 'affine']]

List Elements Can be Accessed by Index

Lists have indices just like strings, and many of the string features you know will work on lists.

You can recall an element from a list by referring to its index in the list using the same square brackets ([]) used with strings.

print( substitution[3] )
print( transposition[0] )
affine
route

If you have a list inside of a list (known as a nested list) you can use two sets of square brackets to refer to its index. The first set of brackets contains the index of the nested list, and the second set of brackets contains the index for the item inside the nested list.

print( allciphers[1][2] )
multiplicative

Negative indices and slicing work with lists too:

print( substitution[:2] )
print( transposition[-2] )
['keyword', 'caesar']
route

Other List Operations

There are other frequently used operations with lists:

operation usage
len( list-name ) len() Returns length of the list
list-name.append(object) .append() appends the object to the end of a list. Will create a nested list if you append a list.
list-name.extend(object) .extend() will take all objects in a second list and add them to the first list. Will not create a nested list.
object in list-name in will return True/False if an object is in / not in a list
object not in list-name not in will return True/False if an object is not in / in a list
min( list-name ) min() returns the smallest element in the list
max( list-name ) max() returns the largest element in the list
list-name[index] = new-value You can change a single element in a list (can't do this with strings)
for i in list-name: You can use a for loop to iterate through only the elements in a list
[1, 2, 3] * 2 The * operation will replicate the list by the number of times specified.

Some common use cases include:

Determining the Length of a List

print( len([1,2,3,4]) )
4

Appending a List

The append operator modifies the list directly, so you don't need to store the new list back to the original list, like we do with strings.

a = [1, 2, 3]
a.append(4)
print( a )
[1, 2, 3, 4]

Notice if you append a list to a list, it will become a nested list.

a = [1, 2, 3]
b = [4, 5, 6]
a.append(b)
print( a )
[1, 2, 3, [4, 5, 6]]

Extending a List

The extend operator will copy all the elements in a second list to the first list individually, avoiding a nested list.

a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print( a )
[1, 2, 3, 4, 5, 6]

You can also extend to a list using the += assignment operator. One thing to note is that using this operator will go through an iterable object one element at a time and append each element to the end of the list. For example, a string will be added one character at a time. A list, will be appended one element at a time.

a = [1, 2, 3]
a += [4, 5, 6]
print( a )
[1, 2, 3, 4, 5, 6]
a = ['h', 'e']
a += 'llo'
print( a )
['h', 'e', 'l', 'l', 'o']

Replicating a List

Replication will allow you to generate a list quickly if there's a pattern that you could use to construct it. For example, if you needed a list of 26 zeros:

myList = [0]*26
print( myList )
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Or the letters a, b, c, in a list 3 times:

myList = ['a', 'b', 'c'] * 3
print( myList )
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']