Python = ["Ten", "Things", "To", "Do", "With", "Lists"]

1. Enumerate


In [ ]:
# conventional method
x_coords =  [101, 102, 103, 104, 105, 111, 107, 112]
y_coords =  [552, 557, 524, 522, 526, 530, 535, 540]
frame_num = [22, 23, 24, 27, 28, 29, 30, 31]

i = 0
for coord in x_coords:
    print(frame_num[i], x_coords[i], y_coords[i])
    i += 1

In [ ]:
for i, coord in enumerate(x_coords):
    print(frame_num[i], x_coords[i], y_coords[i])

2. Zip


In [ ]:
for x, y in zip(x_coords, y_coords):
    print(x, y)

Enumerate + Zip


In [ ]:
# will work with shoter list, but if lists longer than i, will give error
for i, (x, y) in enumerate(zip(x_coords, y_coords)):
    print(frame_num[i], x, y)

Cautions


In [ ]:
x_coords2 =  [101, 102, 103, 104, 105, 111, 107, 112]
y_coords2 =  [552, 557, 524, 522, 526, 530, 535, 540] # test out shorter
frame_num2 = [22, 23, 24, 27, 28, 29, 30, 31]         # test out shorter

for i, (x, y) in enumerate(zip(x_coords, y_coords2)):
    print(frame_num[i], x, y)

3. Strings == Lists


In [25]:
list_of_nums = [1, 2, 3, 4, 5]
print(list_of_nums[1])
print(list_of_nums[0:4])


2
[1, 2, 3, 4]

In [28]:
rna = "AUGGG"
print(rna[0])
print(rna[-1])
print(rna[-1::-1])


A
G
GGGUA

In [30]:
list_of_strings = ["#some comment", "useful data", "#another_comment"]
for string in list_of_strings:
    if string[0] != "#":
        print(string)


useful data

4. Initialising Lists


In [2]:
dna = "AGT" * 25
print(dna)


AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT

In [6]:
dna_list = ["AGT"] * 5
print(dna_list)


['AGT', 'AGT', 'AGT', 'AGT', 'AGT']

In [7]:
long_dna_list = ["AGT" * 20] * 5
print(long_dna_list)


['AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT', 'AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT', 'AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT', 'AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT', 'AGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGTAGT']

In [11]:
empty_list = [None] * 10
print(empty_list)

for i, item in enumerate(empty_list):
    empty_list[i] = 5

print(empty_list)


[None, None, None, None, None, None, None, None, None, None]
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

5. Using the range


In [27]:
for i in range(1, 10):
    print(i)


1
2
3
4
5
6
7
8
9

6. List Comprehensions


In [ ]:
x_coords =  [101, 102, 103, 104, 105, 111, 107, 112]

squared_list = []
for coord in x_coords:
    squared = coord * coord
    squared_list.append(squared)
    
print(squared_list)

In [ ]:
x_coords =  [101, 102, 103, 104, 105, 111, 107, 112]
squared_list = [coord * coord for coord in x_coords]

print(squared_list)

In [ ]:
x_coords =  [101, 102, 103, 104, 105, 111, 107, 112]
squared_list = [coord * coord for coord in x_coords if coord > 110]

print(squared_list)

In [ ]:
x_coords =  [101, 102, 103, 104, 105, 111, 107, 112]
squared_list = [coord * coord if coord > 110 else 0 for coord in x_coords]

print(squared_list)

7. Sets


In [ ]:
duplicate = [1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9]
unique = set(duplicate)
print(unique)

In [ ]:
# Note that I'm using a list of tuples
duplicate_groups = [(1, 2), (2, 1), (3, 4), (1, 2), (3, 3), (3, 3), (2, 2)] # cares about order within tuples
unique_groups = set(duplicate_groups)
print(unique_groups)

Union


In [ ]:
# normal way to join two lists
sports_list1 = ["Tennis", "Cricket", "Rugby", "AFL", "Archery"]
sports_list2 = ["Tennis", "Cricket", "Fencing", "Swimming"]
sports_list1.extend(sports_list2)
print(sports_list1)

In [ ]:
# ensuring you only only have unique elements
sports1 = set(["Tennis", "Cricket", "Rugby", "AFL", "Archery"])
sports2 = set(["Tennis", "Cricket", "Fencing", "Swimming"])

all_sports = sports1.union(sports2)
print(all_sports)

Intersection


In [ ]:
# sports the sets have in common
print(sports1.intersection(sports2))

Difference


In [ ]:
# sports one set has that the other does not
print(sports1.difference(sports2))
print(sports2.difference(sports1))

Symmetric Difference


In [ ]:
# sports neither set have in common
print(sports1.symmetric_difference(sports2))

8. Map


In [ ]:
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def squared(number):
    return number * number

list_of_squared_numbers = []
for number in list_of_numbers:
    squared_number = squared(number)
    list_of_squared_numbers.append(squared_number)

print(list_of_squared_numbers)

In [ ]:
map_squared_numbers = map(squared, list_of_numbers)
print(list(map_squared_numbers))

9. Filter


In [ ]:
new_numbers = [10 , 20 , 30, 40, 50, 60, 70, 80 ,90, 100, 110, 120, 130, 140, 150]

def less_than_100(number):
    if number < 100:
        return True
    else:
        return False

small_numbers = filter(less_than_100, new_numbers)
print(list(small_numbers))

10. Reduce


In [29]:
# moved in Python3
from functools import reduce

other_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

def summing(num1, num2):
    return num1 + num2

sum_of_numbers = reduce(summing, other_numbers)
print(sum_of_numbers)


210

In [ ]: