Date:

Share:

Python Zip Two Lists

Related Articles

introduction

Python has a huge variety of built-in functions that help us quickly implement an algorithm that would otherwise take much longer to create ourselves. The accessibility of these meanings is part of what makes Python such a powerful programming language. One of these useful functions is zip() function.

In this article, we’ll explore how we can compress two lists with Python’s zip() function. We will also look at other concepts like lists, tuples and lesser known use of * symbol, which will allow us to take full advantage of the zip () function.

A brief overview of lists and treatments

A list is a data structure in Python. Usually, in programming, this is known as an array. Lists contain a set of neat elements. We can access impressive individual elements by indexing. for example:

>>> list_x = [1, 2, 'a', 'b']
>>> list_x[0]
1
>>> list_x[2]
'a'
>>> list_x[1]
2

Here, we define list_x Containing integers and strings. We then use the syntax shown in the example to get an object from the list. List indexes start at 0 for the first item in the list. We can even store lists within lists:

>>> list_y = [[1, 2], ['a', 'b']]
>>> list_y[0]
[1, 2]

Tuples are another type of data in Python that are very similar to lists. The difference is that patients are not changeable, unlike lists. This means that we can not modify the content in tuples once it has been configured. for example:

>>> tuple_x = (1, 2, 3)
>>> tuple_x[0]
1
>>> tuple_x[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> 
>>> list_x = [1, 2, 'a', 'b']
>>> 
>>> list_x[0] = 100
>>> print(list_x)
[100, 2, 'a', 'b']

In this example, we see that tuples use parentheses instead of parentheses. We can get treated items just like we do with lists. However, if we try to change items in tuple we get an error. A list however allows us to change its items.

Alternate use of * Icon in Python

If you worked with Python, you probably used the asterisk icon * For the purpose of multiplication. However, you may have also seen the * Symbol used in front of variables. But what does it actually do?

God * You can use a variable opposite operator to define a function or call a function. It takes arguments from the variable (which is supposed to store iterables) and breaks them down as arguments for the function. for example:

>>> def func(x, y):
...     return x, y
... 
>>> list_x = [1, 2]
>>> 
>>> print(func(*list_x))
(1, 2)

Here, we have a function that takes 2 arguments and returns those arguments as treated. With the use of * Operator, we can store the 2 arguments in the list and then unload these arguments directly by calling the function. Using it in a function definition stores all the arguments in the tuple assigned to the variable.

>>> def func1(*args):
...     print(args)
... 
>>> func1(1, 2, 3, 4)
(1, 2, 3, 4)

This operator is often used with the zip () function in Python.

How can we compress two lists in Python?

God zip() A built-in function in Python is used to connect patients or lists. The way the function joins them can best be understood using an example:

>>> zipped_list = zip([1, 2, 3], [3, 4, 5])
>>> print(zipped_list)
<zip object at 0x7fd928b1f540>
>>> list(zipped_list)
[(1, 3), (2, 4), (3, 5)]
>>> 

The example above shows us how two lists can be put together based on the corresponding elements. All we have to do is use both lists as arguments for the zip () function. In this example, we can see exactly what the zip function does. It takes each index from the lists provided as arguments and puts them into a single patient. That is, all index 0 items enter the first tuple, all index 1 items enter the second tuple, and so on.

The output of the zip () function is an iterator, which means we need to implement the list() or tuple() Output functions to access the final joining list. And since the output is an iterator we can use next() Function to achieve the shrinking items one by one, which has its own advantages.

Lists of covers of various lengths

Suppose we have to compress two different lists, but they have different lengths. How does the zip () function handle this situation?

If the list lengths are different, the zip () function will only compress to the point where it can find matching indexes. for example:

>>> list(zip([1, 2, 3, 4], [5, 6]))
[(1, 5), (2, 6)]

Here we can see that the zip () function stops joining indices after index 2 because the second list does not have index 3.

Shrinks more than two lists

We can actually ride together on more than one list. for example:

>>> list(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

This example shows that we can compress more than two lists, simply by adding them to our zip () function. Moreover, as mentioned earlier this can also be done with e * sets off:

>>> list_x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list(zip(*list_x))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> 
>>> list_x.append([10, 11, 12])
>>> list_x.append([13, 14, 15])
>>> list(zip(*list_x))
[(1, 4, 7, 10, 13), (2, 5, 8, 11, 14), (3, 6, 9, 12, 15)]
>>> 

Here, we see that we can use * Operator with the zip () function to compress the lists list_x. Then, we see this after we add two more lists list_x, We can still use the same line of code to compress the lists. This is the advantage of * sets off. We can ride multiple lists together without knowing how many lists we have list_x.

Note that in the previous sections, we have provided list arguments in our examples, but we can do all the same operations with tuple arguments.

What is the inversion of a zip function?

The zip () function is used not only to join multiple sequences, but also to do the opposite: open them. We can do this through the * Operator and output of zip () function with other zip () function as follows:

>>> list_x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list(zip(*list_x))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> list(zip(*[(1, 4, 7), (2, 5, 8), (3, 6, 9)]))
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Here, we see that using a zip on the output of the previous zip operation we can reverse the operation and return our original list. The only difference is that the items on the list are now tuples.

Summary

In this article, we will first briefly go through the lists and treatments in Python. We then investigated alternative use of * symbol. Using our knowledge of these concepts, we tested what the zip () function is in Python and saw different ways to use it. Finally, we tested how we can reverse the whole zip operation.

Next steps

If you are interested in learning more about the basics of Python, coding and software development, please refer to the basics of coding for developers, where we cover the essential languages, concepts and tools you will need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, please feel free to contact jacob@initialcommit.io.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles