Had a couple of over thought and buggy examples yesterday, so removed them. Today Iâ€™ve given it more thought and came up with this algorithm to merge the data at index `[x]`

with the data at index `[len(subjects)]`

. Turns out it is remarkably simple.

```
subjects = ["physics", "calculus", "poetry", "history"]
grades = [98, 97, 85, 88]
n = len(subjects)
c = subjects + grades
print (c)
```

```
['physics', 'calculus', 'poetry', 'history', 98, 97, 85, 88]
```

```
x = 0
while len(c) > n:
c[x] = [c[x]] + [c.pop(n)]
x += 1
print (c)
```

```
[['physics', 98], 'calculus', 'poetry', 'history', 97, 85, 88]
[['physics', 98], ['calculus', 97], 'poetry', 'history', 85, 88]
[['physics', 98], ['calculus', 97], ['poetry', 85], 'history', 88]
[['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]
```

The key operation here is to turn the value at index `[x]`

into a list then extend it with the value popped from index `[n]`

, also as a list.

We could also look at appending the popped value with basic recasting.

```
c[x] = [c[x]]
c[x].append(c.pop(n))
```

And since all the above is spaghetti code (inline), we can abstract it to a function with less verbosity and a direct return.

```
def pair(a, b):
n = len(a)
c = a + b
x = 0
while len(c) > n:
c[x] = [c[x]]
c[x].append(c.pop(n))
x += 1
return c
grade_book = pair(subjects, grades)
print (grade_book)
```

```
[['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]
```

This gives us a pseudo zip object but without the iterator. Itâ€™s still a re-usable list, or rather list of lists to represent a, b pairs. The choice of data structure is ours. As tuple pairs they would be immutable, also a consideration for use case. Tuples are the least expensive primary data structure. The above allows mutation of all the values.

Note the slight change-up in the code for tuplesâ€¦

```
def pair(a, b):
n = len(a)
c = a + b
x = 0
while len(c) > n:
c[x] = (c[x], c.pop(n))
x += 1
return c
grade_book = pair(subjects, grades)
print (grade_book)
```

```
[('physics', 98), ('calculus', 97), ('poetry', 85), ('history', 88)]
```

The above only allows changes to the parent list, but not the tuples it contains.

Now that we mention it, the list version can follow suitâ€¦

```
def pair(a, b):
n = len(a)
c = a + b
x = 0
while len(c) > n:
c[x] = [c[x], c.pop(n)]
x += 1
return c
```

```
[['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88]]
```