For Loop in List

subhabangalore at gmail.com subhabangalore at gmail.com
Sun Jan 13 13:45:39 CET 2013


Dear Group,

I have a list like,

>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12]

Now, if I want to take a slice of it, I can.
It may be done in,
>>> list2=list1[:3]
>>> print list2
[1, 2, 3]

If I want to iterate the list, I may do as,

>>> for i in list1:
	print "Iterated Value Is:",i

	
Iterated Value Is: 1
Iterated Value Is: 2
Iterated Value Is: 3
Iterated Value Is: 4
Iterated Value Is: 5
Iterated Value Is: 6
Iterated Value Is: 7
Iterated Value Is: 8
Iterated Value Is: 9
Iterated Value Is: 10
Iterated Value Is: 11
Iterated Value Is: 12

Now, I want to combine iterator with a slicing condition like

>>> for i=list2 in list1:
	print "Iterated Value Is:",i

So, that I get the list in the slices like,
[1,2,3]
[4,5,6]
[7,8,9]
[10,11,12]

But if I do this I get a Syntax Error, is there a solution?

If anyone of the learned members may kindly let me know?

Apology for any indentation error,etc. 

Thanking You in Advance,

Regards,
Subhabrata 

	





More information about the Python-list mailing list