in

Python zip() : Python Function

python-zip

What is Python Zip() Function?

Python Zip() Definition and Usage:

The zip() function returns a zip object, which is a iterables of tuples where the first item of each passed iterables is joined together, and then another item of each passed iterables is paired, etc.

If the lengths of the passed iterators are different, the iterables with the least items determines the length of the new iterables.

Create a iterables that combines elements from each iterables.

Returns the iterables of tuples, where the i-th tuple contains the i-th component from each argument sequence of iterations. The iterables stop when the short input iterables task is finished. With a single iterative argument, it gives a iterables of 1-tuples. Without any arguments, it simply gives a repeat. In comparison to:

The syntax of the zip() in python function is:

zip(*iterables)

The zip() * operator can be used to unzip the list in conjunction with the operator:

zip(iterator1, iterator2, iterator3 …)

zip() Parameters:

ParameterDes.
iterablescan be built-in iterables (like: string, list, dict) and joined together,
or user-defined iterables

Return value from zip() :

The zip() function returns an array of tuples based on repeat on objects.

If we do not pass a parameter, the zip() returns an empty iterables
If only one iterables passes, the zip() returns iterables of tuples with only one element with each tuple.

If multiple repetitions are passed, the zip() returns to the iterables of the tuples containing the elements of all the repetitions with each tuples.

Suppose, two repetitions are passed on the zip(); One repeat consists of three and the other has five elements. Then, the returnee will have three tuples. This is because the iterator stops when the short repetitive task is finish.

When the name of the variable form package. The module, in general, returns a top-level package (name up to the first name), not a module named by name. However, when a blank listed argument is given, the module named by name returns.

1: Python Zip() :

num_list = [11, 22, 33]
str_list = ['eleven', 'twentytwo', 'thirtythree']

result = zip()

result_list = list(result)
print(result_list)

result = zip(number_list, str_list)

result_set = set(result)
print(result_set)

Output:

[]
{(22, 'twentytwo'), (33, 'thirtythree'), (11, 'eleven')}

2: If a tuple contains more items, these items are ignore :

a = ("Rohit", "Monty", "Johnny")
b = ("Radha", "Mia", "Rosy", "Sonu")

x = zip(a, b)

print(tuple(x))

Output:

(('Rohit', 'Radha'), ('Monty', 'Mia'), ('Johnny', 'Rosy'))

3: Different number of iterable elements :

numbersList = [11, 22, 33]
str_list = ['one', 'two']
numbers_tuple = ('ONE', 'TWO', 'THREE', 'FOUR')

result = zip(numbersList, numbers_tuple)

result_set = set(result)
print(result_set)

result = zip(numbersList, str_list, numbers_tuple)

result_set = set(result)
print(result_set)

Output:

{(22, 'TWO'), (11, 'ONE'), (33, 'THREE')}
{(11, 'one', 'ONE'), (22, 'two', 'TWO')}

* operator can be used for conjunction with zip() to unzip the list.

zip(*zippedList)

4: Unzipping the Value Using zip() :

coordinate = ['a', 'b', 'c']
value = [1, 2, 3]

result = zip(coordinate, value)
result_list = list(result)
print(result_list)

x, y =  zip(*result_list)
print('x =', x)
print('y =', y)

Output:

[('a', 1), ('b', 2), ('c', 3)]
x = ('a', 'b', 'c')
y = (1, 2, 3)

3 Comments

Leave a Reply

3 Pings & Trackbacks

  1. Pingback:

  2. Pingback:

  3. Pingback:

Leave a Reply

Your email address will not be published. Required fields are marked *

raspberry-pi-barcode-scanner

Raspberry pi Barcode Scanner