[Tutor] removing sq. of items.

Dave Angel d at davea.name
Wed May 23 13:27:51 CEST 2012


On 05/23/2012 06:07 AM, Bala subramanian wrote:
> Hi,
> I infact want write each of the item in the sliced list to a file.
This line is top-posted.  Please put your remarks *after* the part
you've quoted.

There isn't one slice, but many of them.  So you have a list of lists.
>
> On Wed, May 23, 2012 at 11:59 AM, Sarma Tangirala <tvssarma.omega9 at gmail.com
>> wrote:
>>
>> On 23 May 2012 15:21, Bala subramanian <bala.biophysics at gmail.com> wrote:
>>
>>> Friends,
>>> While iterating through each list item and printing/writing it, why does
>>> the sq. brackets get printed/written to the file. Just a small eg.code is
>>> given below.
>>>
>>>>>> N=100
>>>>>> myl=range(1,100+1)
>>>>>> new=[myl[i:i+15] for i in range(0, len(myl),15)]
>>>>>> for x in new: print x

Each item in new is a list, since you created it with a slice.  So x is
a list, and when you print it, you get the default representation,
including the square brackets.  If you want to print the individual
items within x, separated by some marker of your choice, then you have
to write such code.

Instead of print x, you might want
       for item in x:
              print item,

This would show the items separated by a space.

Or you might want them separated by a comma:

     print ",".join(x)



-- 

DaveA



More information about the Tutor mailing list