Could we combine the lists using `zip()`?

Why this doesn’t work???

def combine_sort(lst1,lst2)
  new_lst = list(zip(lst1, lst2))
  new_lst.sort()
  return new_lst

First, make sure you put a colon after def combine_sort(lst1, lst2).

Second, the zip function maps elements from the provided iterables together, returning a tuple. So instead of combining the two lists into one new list, as the module requested, you’ve created one new list of multiple tuples, comprised of elements from both original lists:

[(2, 5), (4, -10), (5, 10), (10, 2)]

Notice how your sort function didn’t accurately sort the values, because your list actually has 4 elements instead of the intended 8.

Also, what if the two lists being combined were of varying length? The zip function stops when the shortest input iterable runs out of elements to map against the longer input iterable. Your resulting list would actually leave elements unaccounted for in this case.

1 Like

What’s the difference between:

new_list = list1 + list2

and

new_list = zip(list1, list2) ?

Thanks in advance

First big difference, a single linear list vs. a list of tuples.

If we try to sort a list of tuples, it will be by first element in each tuple, and will still return a list of tuples.

sorted(lst1 + lst2)

will produce a linear sorted list of both combined.

1 Like

In your first example, new_list = list1 + list2, list2, new_list will be a list containing the elements of list2 appended to the end of list1.
For example:

list1 = ['A', 'B', 'C', 'D', 'E']
list2 = [1, 2, 3, 4, 5]

new_list = list1 + list2
print(new_list)

Output:

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, 1, 2, 3, 4, 5]

zip() is an entirely different operation.
It will return a zip object which is actually an iterable. See this post for more on what that means. Basically, you can only iterate through its values once unless you first convert it into a list.
For Example:

list1 = ['A', 'B', 'C']
list2 = [1, 2, 3]

new_list = zip(list1, list2)

print('new_list:', new_list) #shows zip object and memory location

#iterate the iterable, new_list, printing its values
for element in new_list:
  print(element) #each element is a tuple

print('new_list:', new_list) #the zip object still exists in the same location
#try to iterate and print a second time
for element in new_list:
  print('This will not print because new_list is now empty ', element)

#count the number of items in new list   
new_list_count = sum(1 for _ in new_list)
print('new_list_count:', new_list_count) # 0 because new_list is indeed empty

Output:

new_list: <zip object at 0x7f985dae7e08>
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list: <zip object at 0x7f985dae7e08>
new_list_count: 0

If we convert the zip object to a list we can iterate through the list of tuples as many times as we need to:

list1 = ['A', 'B', 'C']
list2 = [1, 2, 3]

#zip the two lists and convert the zip object to a list
new_list = list(zip(list1, list2))

print('new_list:', new_list) #prints the contents of new_list

#iterate the list, new_list, printing its values
for element in new_list:
  print(element) #each element is a tuple

print('new_list:', new_list) #prints the contents again
#try to iterate and print a second time
for element in new_list:
  print(element)

#count the number of items in new_list
#could also simply use len(new_list) instead since
#new_list is now a list
new_list_count = sum(1 for _ in new_list)
print('new_list_count:', new_list_count) # 3 because new_list is not empty

Output:

new_list: [(‘A’, 1), (‘B’, 2), (‘C’, 3)]
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list: [(‘A’, 1), (‘B’, 2), (‘C’, 3)]
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list_count: 3

As @mtf stated there is a big difference.

3 Likes

On the point raised by @midlindner,

you can only iterate through its values once

this is actually very important to know. ZIP is consumed on first iteration of the object (unless it is first cast to a list object).

The real issue is that the lists are not combined, but two separate parallel lists (side by side) with corresponding elements paired up. Totally not applicable here.

Another concern with ZIP when combining lists is that the object will only have the length of the shortest list if they are uneven in length. The outliers are truncated.

3 Likes