Sayantan's Blog On Python Programming

ZIP FUNCTION IN PYTHON TUPLE

ZIP FUNCTION IN PYTHON TUPLE

INTRODUCTION

zip() function in python tuple, takes input from two or more tuple and returns a zipped object which can be converted into a combined tuple, list or dictionary. In zipped object the corresponding index field of a tuple will be paired with the same index field of other tuples.

# ZIP FUNCTION IN PYTHON TUPLE example 1

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of tuple str_tuple ->", str_tuple) 

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of tuple int_tuple ->", int_tuple)

zip_object = zip(int_tuple, str_tuple)

print("\nValue of zipped object ->", zip_object)

ZIP FUNCTION IN PYTHON TUPLE : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, 500)

Value of zipped object -> <zip object at 0x000001CB69850580>

In the above example, we can see that the both str_tuple and int_tuple are zipped. But the zipped object is not readable. We need to convert the zipped object to a tuple, list or a dictionary to make it readable. To convert the zipped object into a tuple we will use the tuple() function.

# ZIP FUNCTION IN PYTHON TUPLE example 2

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of tuple str_tuple ->", str_tuple) 

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of tuple int_tuple ->", int_tuple)

zip_object = zip(int_tuple, str_tuple)

print("\nValue of zipped object ->", zip_object)

zip_Tuple = tuple(zip_object)
print("\nFields of tuple zip_Tuple ->", zip_Tuple) 

ZIP FUNCTION IN PYTHON TUPLE : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, 500)

Value of zipped object -> <zip object at 0x000001EDE7440600>

Fields of tuple zip_Tuple -> ((400, 'Python'), (200, 'Java'), (600, 'C++'), (100, 'C'), (150, 'PHP'), (500, 'XML'))

In the above example, we can see that the pair has been created post tuple conversion. Each pair is also a tuple.

We can also extract the original tuples from the combined tuple by using zip().

# ZIP FUNCTION IN PYTHON TUPLE example 3

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of tuple str_tuple ->", str_tuple) 

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of tuple int_tuple ->", int_tuple)

zip_object = zip(int_tuple, str_tuple)

print("\nValue of zipped object ->", zip_object)

zip_Tuple = tuple(zip_object)
print("\nFields of tuple zip_Tuple ->", zip_Tuple) 


str_tuple_rev, int_tuple_rev = zip(*zip_Tuple)

print("\nFields of tuple str_tuple_rev ->", str_tuple_rev) 
print("\nFields of tuple int_tuple_rev ->", int_tuple_rev) 

ZIP FUNCTION IN PYTHON TUPLE : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, 500)

Value of zipped object -> <zip object at 0x0000021959730680>

Fields of tuple zip_Tuple -> ((400, 'Python'), (200, 'Java'), (600, 'C++'), (100, 'C'), (150, 'PHP'), (500, 'XML'))

Fields of tuple str_tuple_rev -> (400, 200, 600, 100, 150, 500)

Fields of tuple int_tuple_rev -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')

In the above example, str_tuple_rev is in sync with original tuple str_tuple and int_tuple_rev is in sync with original tuple int_tuple.

Using zip() for more than two tuples.

# ZIP FUNCTION IN PYTHON TUPLE example 4

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of tuple str_tuple ->", str_tuple) 

int_tuple = (400, 200, 600, 100, 150, 500) 
print("Fields of tuple int_tuple ->", int_tuple)

float_tuple = (4.2, 2.6, 6.5, 1.9, 15.3, 5.8) 
print("Fields of tuple float_tuple ->", float_tuple)

bool_tuple = (True, False, True) 
print("Fields of tuple float_tuple ->", float_tuple)

zip_object = zip(int_tuple, str_tuple, float_tuple, bool_tuple)

print("\nValue of zipped object ->", zip_object)

zip_Tuple = tuple(zip_object)
print("\nFields of tuple zip_Tuple ->", zip_Tuple) 


str_tuple_rev, int_tuple_rev, float_tuple_rev, bool_tuple_rev = zip(*zip_Tuple)

print("\nFields of tuple str_tuple_rev ->", str_tuple_rev) 
print("Fields of tuple int_tuple_rev ->", int_tuple_rev) 
print("Fields of tuple float_tuple_rev ->", float_tuple_rev) 
print("Fields of tuple bool_tuple_rev ->", bool_tuple_rev)

ZIP FUNCTION IN PYTHON TUPLE : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')
Fields of tuple int_tuple -> (400, 200, 600, 100, 150, 500)
Fields of tuple float_tuple -> (4.2, 2.6, 6.5, 1.9, 15.3, 5.8)
Fields of tuple float_tuple -> (4.2, 2.6, 6.5, 1.9, 15.3, 5.8)

Value of zipped object -> <zip object at 0x0000023FE1F407C0>

Fields of tuple zip_Tuple -> ((400, 'Python', 4.2, True), (200, 'Java', 2.6, False), (600, 'C++', 6.5, True))

Fields of tuple str_tuple_rev -> (400, 200, 600)
Fields of tuple int_tuple_rev -> ('Python', 'Java', 'C++')
Fields of tuple float_tuple_rev -> (4.2, 2.6, 6.5)
Fields of tuple bool_tuple_rev -> (True, False, True)

In the above example, we can see that the bool_tuple is having only three fields. That’s why after unzipping all the tuples, each tuple is having only three fields.

Now we are modifying the bool_tuple to six field tuple and will see the impact.

# ZIP FUNCTION IN PYTHON TUPLE example 5

str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML') 
print("\nFields of tuple str_tuple ->", str_tuple) 

int_tuple = (400, 200, 600, 100, 150, 500) 
print("Fields of tuple int_tuple ->", int_tuple)

float_tuple = (4.2, 2.6, 6.5, 1.9, 15.3, 5.8) 
print("Fields of tuple float_tuple ->", float_tuple)

bool_tuple = (True, False, True, True, False, False) 
print("Fields of tuple float_tuple ->", float_tuple)

zip_object = zip(int_tuple, str_tuple, float_tuple, bool_tuple)

print("\nValue of zipped object ->", zip_object)

zip_Tuple = tuple(zip_object)
print("\nFields of tuple zip_Tuple ->", zip_Tuple) 


str_tuple_rev, int_tuple_rev, float_tuple_rev, bool_tuple_rev = zip(*zip_Tuple)

print("\nFields of tuple str_tuple_rev ->", str_tuple_rev) 
print("Fields of tuple int_tuple_rev ->", int_tuple_rev) 
print("Fields of tuple float_tuple_rev ->", float_tuple_rev) 
print("Fields of tuple bool_tuple_rev ->", bool_tuple_rev)

ZIP FUNCTION IN PYTHON TUPLE : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')
Fields of tuple int_tuple -> (400, 200, 600, 100, 150, 500)
Fields of tuple float_tuple -> (4.2, 2.6, 6.5, 1.9, 15.3, 5.8)
Fields of tuple float_tuple -> (4.2, 2.6, 6.5, 1.9, 15.3, 5.8)

Value of zipped object -> <zip object at 0x00000237DBD40840>

Fields of tuple zip_Tuple -> ((400, 'Python', 4.2, True), (200, 'Java', 2.6, False), (600, 'C++', 6.5, True), (100, 'C', 1.9, True), (150, 'PHP', 15.3, False), (500, 'XML', 5.8, False))

Fields of tuple str_tuple_rev -> (400, 200, 600, 100, 150, 500)
Fields of tuple int_tuple_rev -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML')
Fields of tuple float_tuple_rev -> (4.2, 2.6, 6.5, 1.9, 15.3, 5.8)
Fields of tuple bool_tuple_rev -> (True, False, True, True, False, False)

Now we can see that all the tuples have six fields.

ZIP FUNCTION IN PYTHON TUPLE : Output
ZIP FUNCTION IN PYTHON TUPLE : Output

RELATED TOPICS:

Leave a Comment

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