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.
In [240]:
# Define a list of integers
number_list = [3, 2, 1, 3, 5, 9, 6, 3, 9]
number_list
Out[240]:
In [241]:
# List can contain strings
word_list = ['Jan', 'Feb', 'Mar', 'Apr']
word_list
Out[241]:
In [242]:
# List can contain mixed data types
mixed_list = [1, 'Jan', 2, 'Feb', 3, 'Mar']
mixed_list
Out[242]:
In [243]:
# Lists can be n-dimensional or list of lists of...
matrix_list = [[1, 'Jan'], [2, 'Feb'], [3, 'Mar']]
matrix_list
Out[243]:
In [244]:
list_3d = [[1, 'Jan', ['Mon', 'Tue']],
[2, 'Feb', ['Wed', 'Thu']],
[3, 'Mar', ['Fri', 'Sat']]]
list_3d
Out[244]:
In [245]:
# First item on the list starts at 0 index
number_list[0]
Out[245]:
In [246]:
# Last item on the list starts at -1 index
number_list[-1]
Out[246]:
In [247]:
# Refer list of lists items using index
list_3d[2][2][0]
Out[247]:
In [248]:
# String is a list of characters, can refer using index
print(word_list[1], word_list[1][1])
In [249]:
# Slicing first 6 items in the list
number_list[:6]
Out[249]:
In [250]:
# Last 4 items in the list
number_list[-4:]
Out[250]:
In [251]:
# 3 items (5-2) starting from 2 index
number_list[2:5]
Out[251]:
In [252]:
# Find first index of an item
number_list.index(9)
Out[252]:
In [253]:
# Length of list
len(number_list)
Out[253]:
In [254]:
# Count of items in the list
number_list.count(9)
Out[254]:
In [255]:
# Max item in list
max(number_list)
Out[255]:
In [256]:
# Min item in list
min(number_list)
Out[256]:
In [257]:
# Append an item to the list
print(number_list)
number_list.append(10)
number_list
Out[257]:
In [258]:
# Extend list with another list
number_list.extend([13, 15, 15, 12])
number_list
Out[258]:
In [259]:
# Insert at index i, an item n to the list (i, n)
number_list.insert(5, 19)
number_list
Out[259]:
In [260]:
# Remove first occurance of an item from the list
number_list.remove(9)
number_list
Out[260]:
In [261]:
# Remove at index, an item from the list, return removed item
last_item = number_list.pop(5)
print(number_list)
last_item
Out[261]:
In [262]:
# Delete a slice from the list
del number_list[1:3]
number_list
Out[262]:
In [263]:
# Replace an item in the list
number_list[0] = 7
number_list
Out[263]:
In [264]:
# Replace a slice in the list
number_list[:3] = [31, 42, 91]
number_list
Out[264]:
In [265]:
# Sort list
number_list.sort()
number_list
Out[265]:
In [266]:
# Reverse list
number_list.reverse()
number_list
Out[266]:
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
Out[267]:
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]:
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]:
In [270]:
# Reduce list to one item
def total(x, y): return (x + y)
totalled = reduce(total, number_list)
totalled
Out[270]:
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]:
In [272]:
# Pop one item from the end or top of the stack
popped_item = stack.pop()
print(stack)
popped_item
Out[272]:
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]:
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]:
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]:
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]:
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]:
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]:
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]:
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]:
In [285]:
unpacked_days, unpacked_times = zip(*packed)
print(unpacked_days)
unpacked_times
Out[285]:
In [286]:
# Transpose a matrix of lists
print(matrix_list)
zip(*matrix_list)
Out[286]:
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.
x in seq
and y not in seq
.seq1 + seq2
and multiply seq1 * n
.There are some notable differences among various sequence types.
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.
pop()
works well.