Tips and Tricks for Python Lists

Kasey Mallette
Nerd For Tech
Published in
4 min readApr 23, 2021

Python lists are data structures used to store and change items. If you’re just learning Python or want to learn some neat tricks when using lists, this post is for you! The documentation on python lists can be found here.

1. Element-wise addition of two lists

LaTeXiT

In Python, if we add two lists together, we get a list that combines the elements from both lists into a single list.

a = [a1,a2,a3]
b = [b1,b2,b3]
c = a + b
print('a + b = ', c)

a + b = [a1, a2, a3, b1, b2, b3]

In order to add the lists element-wise, we need to iterate over both lists. A for loop can be a great way to iterate over a list. However, if we create two for loops to try to iterate over both lists we get the following:

for x in a:
for y in b:
z = x + y
print(z)

❗️ This will return all possible combinations of x + y and will not provide the solution we need.

Rather, we need to use the zip function, which aggregates the elements of the lists we are iterating over.

for x, y in zip(a, b):
z = x + y
print(z)

In order to store the results of the element-wise addition, we can use the method .append() to add the results to a new list.

results = []
for x, y in zip(a, b):
z = x + y
results.append(z)

Take a look at the solution below. ⬇️

def add(list_1, list_2, solution):
for x, y in zip(list_1, list_2):
z = x + y
solution.append(z)
return solution
a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = []
add(a, b, c)

[7, 9, 11, 13, 15]

2. Create a dictionary from two lists

Dictionaries, which are indexed by keys, can be created using two lists: a list of the keys and a list of the values.

def zip_dict(keys, values, new_dict):
for key, value in zip(keys, values):
new_dict[key] = value
return new_dict
keys = ['key_1', 'key_2', 'key_3']
values = [1, 2, 3]
new_dict = {}
zip_dict(keys, values, new_dict)

{‘key_1’: 1, ‘key_2’: 2, ‘key_3’: 3}

3. Partition a list into a list of lists

We can split a list into groups by partitioning the list into lists of n elements. Given a list, and a number to group by, we can use the range function to divide the list. We can then append items from the list for every n items.

def partition(list_1, new_list, n):
for i in range(0, len(list_1), n):
group = list_1[i:i+n]
new_list.append(group)
return new_list
list_1 = [1, 2, 3, 4, 5, 6, 7]
new_list = []
n = 2
partition(list_1, new_list, n)

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

4. Partition a list into a list of tuples

Tuples are another data structure in Python that are immutable and cannot be changed. Just as we can partition a list into a list of lists, we can partition a list into a list of tuples.

def tup_partition(list_1, new_list, n):
for i in range(0, len(list_1), n):
group = list_1[i:i+n]
tup_group = tuple(group)
new_list.append(tup_group)
return new_list
list_2 = [1,3,2,4,6,5]
new_list = []
n = 2
tup_partition(list_2, new_list, n)

[(1, 3), (2, 4), (6, 5)]

5. Flatten a list of lists into a single list

In Python, we can take a list of lists and flatten it into a list of all of the elements. We can create a function that takes in a list of lists, and a new, empty list, and outputs the list of lists by appending each item to the new list.

def flatten(lists, new_list):
for list in lists:
for item in list:
new_list.append(item)

return new_list

lists = [[1, 2], [3, 4], [5, 6], [7]]
new_list = []
flatten(lists, new_list)

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

6. Copy a list and make changes

In Python, if we set a list equal to another list, such that a = b, and make changes to a, the changes will apply to b as well. In order to copy a list, and change only one list and not both, we need to use the .copy() method.

a = [1,2,3]b = a.copy()
c = b.copy()

a.remove(2) # Use .remove() to delete a specific element
b.pop(0) # Use .pop() to delete an element at a specific index
print('a = ', a)
print('b = ', b)
print('c = ', c)

a = [1, 3]

b = [2, 3]

c = [1, 2, 3]

7. Clear a list

Rather than removing each item at a time, we can use the .clear() method to remove all items from a list.

a = [1, 3]
b = [2, 3]
a.clear()
b.clear()
b.append('done')print(a)
print(b)

[]

[‘done’]

--

--