iterator object by using zip

We can input a set of iterables and zip function will return an object.
name=['Ravi','Raju','Alex']
id=[1,2,3]
MATH=[30,40,50]
my_data=zip(name,id,MATH) # zip object 
print(list(my_data))
Output
[('Ravi', 1, 30), ('Raju', 2, 40), ('Alex', 3, 50)]
We used list in above code.
We can use one set
print(set(my_data))
Output
{('Alex', 3, 50), ('Ravi', 1, 30), ('Raju', 2, 40)}
Using tuple
print(tuple(my_data))
Output
(('Ravi', 1, 30), ('Raju', 2, 40), ('Alex', 3, 50))

zip with different number of elements

We can create up to the shortest element available. Here we have used 2 number of IDs in place of 3
name=['Ravi','Raju','Alex']
id=[1,2] 
MATH=[30,40,50]
my_data=zip(name,id,MATH)
print(list(my_data))
Output
[('Ravi', 1, 30), ('Raju', 2, 40)]
* operator can be used to unzip
name=['Ravi','Raju','Alex']
id=[1,2,3] 
MATH=[30,40,50]
my_data=zip(name,id,MATH)
name,id,mark=zip(*my_data)
print(name)
print(id)
print(mark)
Output
('Ravi', 'Raju', 'Alex')
(1, 2, 3)
(30, 40, 50)


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer