Python zip function

input: iteratables or containers (list, string) of the same lengths

return value: an iterator with mapped values of ith elements of the iteratables or containers (list, string) 

 

SCRIPT

# initializing lists

name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]

roll_no = [ 4, 1, 3, 2 ]

marks = [ 40, 50, 60, 70 ]

  

# using zip() to map values

mapped = zip(name, roll_no, marks)

  

# converting values to print as a set

mapped = set(mapped)

  

# printing resultant values 

print ("The zipped result is : ",end="")

print (mapped)

 

OUTPUT

The zipped result is : {('Shambhavi', 3, 60), ('Astha', 2, 70), ('Manjeet', 4, 40), ('Nikhil', 1, 50)}

 

SCRIPT: UNZIP

namz, roll_noz, marksz = zip(*mapped)

print ("The unzipped result: \n",end="")

  

# printing initial lists

print ("The name list is : ",end="")

print (namz)

print ("The roll_no list is : ",end="")

print (roll_noz)

print ("The marks list is : ",end="")

print (marksz)

 

OUTPUT: UNZIP

The unzipped result:

The name list is : ('Manjeet', 'Nikhil', 'Shambhavi', 'Astha')

The roll_no list is : (4, 1, 3, 2)

The marks list is : (40, 50, 60, 70)

 

[ref: https://www.geeksforgeeks.org/zip-in-python/]

 

 

'Programming' 카테고리의 다른 글

[python] Passing function with partial arguments  (0) 2019.09.25
[Linux] 메모리 관리  (0) 2018.05.22
[Linux] grep / find 사용하기  (0) 2018.04.26
R 기초  (0) 2018.01.24
[linux][펌] ctags 사용법  (0) 2017.09.26

+ Recent posts