Wednesday, April 18, 2012

list operations using list comprehension

I have a list.



a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]


I want to use list comprehension & wanted to create output as :



output1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]

output2:
('value', 1)
('value', 2)
'
'
('value', 20)


I can create output1 and output2 using for loop but I dont have idea that how I can use list comprehension for the same.



If any one knows this, kindly let me know.



thanks in advance.





No comments:

Post a Comment