List in Python

This is a companion notebook to the Data Science Solutions book.

List is one of the fundamental and powerful data structures natively available in Python. Understanding how List works, will help us understand rest of the Python data structures.

Learning goals in this notebook.

  • Defining a list
  • Traversing a list
  • Changing items in a list
  • Functional programming with lists
  • Stacks as lists
  • Create lists using comprehensions
  • Packing and unpacking lists
  • Sequences in Python
  • Performance of lists

Defining a list

You can easily define a list as a comma-separated set of values within square [ ] brackets.


In [240]:
# Define a list of integers
number_list = [3, 2, 1, 3, 5, 9, 6, 3, 9]
number_list


Out[240]:
[3, 2, 1, 3, 5, 9, 6, 3, 9]

In [241]:
# List can contain strings
word_list = ['Jan', 'Feb', 'Mar', 'Apr']
word_list


Out[241]:
['Jan', 'Feb', 'Mar', 'Apr']

In [242]:
# List can contain mixed data types
mixed_list = [1, 'Jan', 2, 'Feb', 3, 'Mar']
mixed_list


Out[242]:
[1, 'Jan', 2, 'Feb', 3, 'Mar']

In [243]:
# Lists can be n-dimensional or list of lists of...
matrix_list = [[1, 'Jan'], [2, 'Feb'], [3, 'Mar']]
matrix_list


Out[243]:
[[1, 'Jan'], [2, 'Feb'], [3, 'Mar']]

In [244]:
list_3d = [[1, 'Jan', ['Mon', 'Tue']], 
           [2, 'Feb', ['Wed', 'Thu']], 
           [3, 'Mar', ['Fri', 'Sat']]]
list_3d


Out[244]:
[[1, 'Jan', ['Mon', 'Tue']],
 [2, 'Feb', ['Wed', 'Thu']],
 [3, 'Mar', ['Fri', 'Sat']]]

Traversing a list

List traversal includes syntax to learn more about, find, and slice items in a list.


In [245]:
# First item on the list starts at 0 index
number_list[0]


Out[245]:
3

In [246]:
# Last item on the list starts at -1 index
number_list[-1]


Out[246]:
9

In [247]:
# Refer list of lists items using index
list_3d[2][2][0]


Out[247]:
'Fri'

In [248]:
# String is a list of characters, can refer using index
print(word_list[1], word_list[1][1])


('Feb', 'e')

In [249]:
# Slicing first 6 items in the list
number_list[:6]


Out[249]:
[3, 2, 1, 3, 5, 9]

In [250]:
# Last 4 items in the list
number_list[-4:]


Out[250]:
[9, 6, 3, 9]

In [251]:
# 3 items (5-2) starting from 2 index
number_list[2:5]


Out[251]:
[1, 3, 5]

In [252]:
# Find first index of an item
number_list.index(9)


Out[252]:
5

In [253]:
# Length of list
len(number_list)


Out[253]:
9

In [254]:
# Count of items in the list
number_list.count(9)


Out[254]:
2

In [255]:
# Max item in list
max(number_list)


Out[255]:
9

In [256]:
# Min item in list
min(number_list)


Out[256]:
1

Changing items in a list

Python List is mutable. Python provides methods on List objects for changing the items within the list.


In [257]:
# Append an item to the list
print(number_list)
number_list.append(10)
number_list


[3, 2, 1, 3, 5, 9, 6, 3, 9]
Out[257]:
[3, 2, 1, 3, 5, 9, 6, 3, 9, 10]

In [258]:
# Extend list with another list
number_list.extend([13, 15, 15, 12])
number_list


Out[258]:
[3, 2, 1, 3, 5, 9, 6, 3, 9, 10, 13, 15, 15, 12]

In [259]:
# Insert at index i, an item n to the list (i, n)
number_list.insert(5, 19)
number_list


Out[259]:
[3, 2, 1, 3, 5, 19, 9, 6, 3, 9, 10, 13, 15, 15, 12]

In [260]:
# Remove first occurance of an item from the list
number_list.remove(9)
number_list


Out[260]:
[3, 2, 1, 3, 5, 19, 6, 3, 9, 10, 13, 15, 15, 12]

In [261]:
# Remove at index, an item from the list, return removed item
last_item = number_list.pop(5)
print(number_list)
last_item


[3, 2, 1, 3, 5, 6, 3, 9, 10, 13, 15, 15, 12]
Out[261]:
19

In [262]:
# Delete a slice from the list
del number_list[1:3]
number_list


Out[262]:
[3, 3, 5, 6, 3, 9, 10, 13, 15, 15, 12]

In [263]:
# Replace an item in the list
number_list[0] = 7
number_list


Out[263]:
[7, 3, 5, 6, 3, 9, 10, 13, 15, 15, 12]

In [264]:
# Replace a slice in the list
number_list[:3] = [31, 42, 91]
number_list


Out[264]:
[31, 42, 91, 6, 3, 9, 10, 13, 15, 15, 12]

In [265]:
# Sort list
number_list.sort()
number_list


Out[265]:
[3, 6, 9, 10, 12, 13, 15, 15, 31, 42, 91]

In [266]:
# Reverse list
number_list.reverse()
number_list


Out[266]:
[91, 42, 31, 15, 15, 13, 12, 10, 9, 6, 3]

Functional programming with lists

Using filter, map, and reduce functional programming methods with List can be a powerful and concise coding technique for list operations.


In [267]:
# Filter list to apply functional checks on all list items
def odds(x): return (x % 2) != 0
odds_list = filter(odds, number_list)
print(number_list)
odds_list


[91, 42, 31, 15, 15, 13, 12, 10, 9, 6, 3]
Out[267]:
[91, 31, 15, 15, 13, 9, 3]

In [268]:
# Map list to apply operations on all list items
def squares(x): return (x ** 2)
squares_list = map(squares, number_list)
squares_list


Out[268]:
[8281, 1764, 961, 225, 225, 169, 144, 100, 81, 36, 9]

In [269]:
# Map multiple lists to combine into one
def divide(x, y): return (x / y)
original_list = map(divide, squares_list, number_list)
original_list


Out[269]:
[91, 42, 31, 15, 15, 13, 12, 10, 9, 6, 3]

In [270]:
# Reduce list to one item
def total(x, y): return (x + y)
totalled = reduce(total, number_list)
totalled


Out[270]:
247

Stacks as lists

Using the List methods append() and pop() you can easily implement a last-in, first-out Stack data structure.


In [271]:
stack = number_list

# Push two items at the end or top of the stack
stack.append(21)
stack.append(23)
stack


Out[271]:
[91, 42, 31, 15, 15, 13, 12, 10, 9, 6, 3, 21, 23]

In [272]:
# Pop one item from the end or top of the stack
popped_item = stack.pop()
print(stack)
popped_item


[91, 42, 31, 15, 15, 13, 12, 10, 9, 6, 3, 21]
Out[272]:
23

Create lists using comprehensions

List comprehensions are concise way to create new lists where items of the new list are a result of applying an operation, method, or function on all list items.


In [273]:
# Create new list of squares for first n integers
n = 10
squares_n = [x**2 for x in range(n)]
squares_n


Out[273]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [274]:
# Create new list of squares for first n odd integers
n = 10
squares_odd_n = [x**2 for x in range(n) if (x % 2) != 0]
squares_odd_n


Out[274]:
[1, 9, 25, 49, 81]

In [275]:
# Create new list by applying a method on items from another
smaller_words = [x.lower() for x in word_list]
smaller_words


Out[275]:
['jan', 'feb', 'mar', 'apr']

In [276]:
# Create new list by applying a function on items from another
absolute_list = [abs(x) for x in [-3, -5, 1, 4, 7]]
absolute_list


Out[276]:
[3, 5, 1, 4, 7]

In [277]:
# Return a list of lists with original and new item
capitalize_words = [[x, x.upper()] for x in word_list]
capitalize_words


Out[277]:
[['Jan', 'JAN'], ['Feb', 'FEB'], ['Mar', 'MAR'], ['Apr', 'APR']]

In [278]:
# Return list applying operation on each item combination from two lists
combine_list = [x * y for x in [1, 2, 3] for y in [3, 1, 4]]
combine_list


Out[278]:
[3, 1, 4, 6, 2, 8, 9, 3, 12]

In [279]:
# Return list of lists for matching criteria between two lists
match_list = [[x, y] for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
match_list


Out[279]:
[[1, 3], [1, 4], [2, 3], [2, 1], [2, 4], [3, 1], [3, 4]]

Packing and unpacking lists

Lists can be packed and unpacked into tuples using the zip() function.


In [284]:
# Pack and then unpack two lists
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
open_times = ['9:30', '8:30', '8:30', '10:30', '11:00']
packed = zip(days, open_times)
packed


Out[284]:
[('Mon', '9:30'),
 ('Tue', '8:30'),
 ('Wed', '8:30'),
 ('Thu', '10:30'),
 ('Fri', '11:00')]

In [285]:
unpacked_days, unpacked_times = zip(*packed)
print(unpacked_days)
unpacked_times


('Mon', 'Tue', 'Wed', 'Thu', 'Fri')
Out[285]:
('9:30', '8:30', '8:30', '10:30', '11:00')

In [286]:
# Transpose a matrix of lists
print(matrix_list)
zip(*matrix_list)


[[1, 'Jan'], [2, 'Feb'], [3, 'Mar']]
Out[286]:
[(1, 2, 3), ('Jan', 'Feb', 'Mar')]

Sequences in Python

Sequence types in Python include strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. Sequence types share many operations in common, so knowing one can ease the understanding of how to work with others.

Many operations shared by sequences.

  • operations x in seq and y not in seq.
  • concat seq1 + seq2 and multiply seq1 * n.
  • sequence traversal operations including return item by index, slicing.
  • len, max, min, count methods.

There are some notable differences among various sequence types.

  • List is mutable, Tuple is immutable.
  • List is denoted with square [ ] brackets, Tuple with round ( ) brackets.

Performance of lists

Lists are generally very fast considering they are native data structure within Python, however certain operations are not recommended on lists. Instead you may want to use other Python or vendor provided specialised libraries for these operations.

  • DO Mutable operations at end of the list are fast. So using pop() works well.
  • DO Implementing Stack data structure as a List works well.
  • NOT Mutable operations at start or middle of the list are slow as these require shifting entire contents of the list. Use Python collections library instead.
  • NOT Operations on entire list contents are slow. Use NumPy to perform operations on entire list.
  • NOT Matrices or tables using list of lists is not optimal. Use Pandas to create Dataframe data structure instead.