#

#

Welcome

sampleqa.in tutorials, python tutorial,python 3 tutorial

Python tutorial


Python Zip Method

    zip built-in function takes optional iterable objects as input, combines one element from each iterable, returns a tuple. This process continues untill shortest iterable exhausted. Zip is a Iterable objects, by default where element is a tuple.

zip has no elements
zip has one iterable argument
zip has two or more iterables arguments

Zip has no arguments

		>>>z = zip()
		>>>z
		
	

Zip has one iterable arguments

		>>>z = zip('12345')
		>>> z
		
		>>> list(z)
		[('1',), ('2',), ('3',), ('4',), ('5',)]
	

Zip has two or more iterable arguments

    zip takes one element at a time from each iterable, combines them as a tuple.There are 5 elements from each list, zip returns five tupples to the list.

		>>>student=["ram","mike","dick","jhon","swati"]
		>>>scores=[90,30,60,90,55]
		>>> z=zip(student,scores)
		>>> list(z)
		[('ram', 90), ('mike', 30), ('dick', 60), ('jhon', 90), ('swati', 55)]
		>>>		
	

Converting 2 lists into a Dictionary using ZIP method

			>>>students=["ram","mike","dick","jhon","swati"]
			>>>scores=[90,30,60,90,55]
			>>>z=zip(students,scores)
			>>>
			>>>d=dict()
			>>>
			>>>for name,score in z:
			    d[name]=score
			>>>
			>>>d
			{'ram': 90, 'mike': 30, 'dick': 60, 'jhon': 90, 'swati': 55}

		

ADS