Python 1D, 2D Lists#

1D List#

Basic#

Create a 1D list#

# Create a 1d list with 5 elements [1,2,3,4,5] using list comprehension
my_list = [i for i in range(1,6)]
print(my_list)

Hide code cell output

[1, 2, 3, 4, 5]

Sum of 1D list#

# Sum of a 1d list
my_list_sum = sum(my_list)
print(my_list_sum)

Hide code cell output

15

Min and Max in 1D list#

# Find min/max in a 1d list
my_list = [1,2,3,4,5]
min_val = min(my_list)
max_val = max(my_list)
print(min_val, max_val)

Hide code cell output

1 5

Average of 1D list#

# Average of a 1d list
my_list = [1,2,3,4,5]
avg = sum(my_list) / len(my_list)
print(avg)

Hide code cell output

3.0

Sort 1D list#

# Sort a 1d list ascendingly/descendingly
my_list = [5,3,1,4,2]
ascending, descending = sorted(my_list, reverse=False), sorted(my_list, reverse=True)
print(ascending, descending)

Hide code cell output

[1, 2, 3, 4, 5] [5, 4, 3, 2, 1]

Sum of even and odd numbers#

# Sum of even/odd numbers in a 1d list
my_list = [1,2,3,4,5]
even = sum(list(filter(lambda x: x % 2 == 0, my_list)))
odd = sum(list(filter(lambda x: x % 2 != 0, my_list)))
print(even, odd)

Hide code cell output

6 9

Advanced#

Find even/odd numbers and their sums#

# Find even/odd numbers and sum of each
my_list = [1,2,3,4,5,6,7,8,9,10]
even_numbers = list(filter(lambda x: x % 2 == 0, my_list))
sum_even = sum(even_numbers)
odd_numbers = list(filter(lambda x: x % 2 != 0, my_list))
sum_odd = sum(odd_numbers)
print(even_numbers, sum_even, odd_numbers, sum_odd)

Hide code cell output

[2, 4, 6, 8, 10] 30 [1, 3, 5, 7, 9] 25

Sum of primes in 1D list#

import math
# Sum of prime numbers in a 1d list
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

my_list = [2,3,4,5,6,7,8,9,10]
prime_numbers = list(filter(is_prime, my_list))
sum_prime = sum(prime_numbers)
print(prime_numbers, sum_prime)

Hide code cell output

[2, 3, 5, 7] 17

Sum of positives and product of negatives#

# Sum of positive numbers and product of negatives numbers
my_list = [-1,2,-3,4,-5,6]
positive_numbers = [x for x in my_list if x > 0]
negative_numbers = [x for x in my_list if x < 0]
sum_positive = sum(positive_numbers)
product_negative = 1
for neg in negative_numbers:
    product_negative *= neg
print(sum_positive, product_negative)

Hide code cell output

12 -15

Sum after removing duplicates#

# Sum of a 1d list after eliminating duplicates
my_list = [1, 2, 3, 2, 4, 5, 1, 6, 7, 8, 5]
unique_numbers = list(set(my_list))
sum_unique = sum(unique_numbers)
print(unique_numbers, sum_unique)

Hide code cell output

[1, 2, 3, 4, 5, 6, 7, 8] 36

2D List#

Basic#

Sum of 2D list#

# Sum of elements in a 2d list
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
total = sum([sum(sub_list) for sub_list in my_list])
print(total)

Hide code cell output

45

Min and Max in 2D list#

# Find max/min in a 2d list
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
max_value = max([max(sub_list) for sub_list in my_list])
min_value = min([min(sub_list) for sub_list in my_list])
print(min_value, max_value)  # order matches heading

Hide code cell output

9 1

Average of 2D list#

# Average of a 2d list
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
total = sum([sum(sub_list) for sub_list in my_list])
average = total / (len(my_list)*len(my_list[0]))
print(average)

Hide code cell output

5.0

Sort sublists in 2D list#

# Sort sublists in a 2d list ascending/descending
my_list = [[5, 3, 1], [4, 2, 6], [9, 7, 8]]
ascending = [sorted(sublist) for sublist in my_list]
descending = [sorted(sublist, reverse=True) for sublist in my_list]
print(ascending, descending)

Hide code cell output

[[1, 3, 5], [2, 4, 6], [7, 8, 9]] [[5, 3, 1], [6, 4, 2], [9, 8, 7]]

Advanced#

Row sums and total sum#

# Sum of rows and sum of (sum of rows)
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row_sums = [sum(row) for row in my_list]
total_sum = sum(row_sums)
print(row_sums, total_sum)

Hide code cell output

[6, 15, 24] 45

Column sums and total sum#

# Sum of each column and sum of (sum of each column)
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column_sums = [sum(row[c] for row in my_list) for c in range(len(my_list[0]))]
total_sum = sum(column_sums)
print(column_sums, total_sum)

Hide code cell output

[12, 15, 18] 45

Max value and its indices in 2D list#

# Find max value and its indices in a 2d list
my_list = [[1, 2, 3], [4, 9, 6], [7, 8, 5]]
max_value = max([max(row) for row in my_list])
for idx, row in enumerate(my_list):
    if max_value in row:
        max_row, max_col = idx, row.index(max_value)
print(max_value, max_row, max_col)

Hide code cell output

9 1 1

Max value and row containing it#

# Find max value and the row containing it in a 2d list
my_list = [[1, 2, 3], [4, 9, 6], [7, 8, 5]]
rows_max = [max(row) for row in my_list]
max_value = max(rows_max)
max_row = my_list[rows_max.index(max_value)]
print(max_value, max_row)

Hide code cell output

9 [4, 9, 6]