Question about object

6 views
Skip to first unread message

sathyamoorthy v

unread,
May 28, 2012, 11:58:40 AM5/28/12
to Chennaipy
Hi,

i have created list 'a'
>>> a = [1.2,3,4,0.2,5,10]

after that i did reverse of the list using reversed function. But
output is like 'list reverse object'

>>> a = reversed(a)
<listreverseiterator object at 0x01DD1730>


please help me to see how to get to see the actual reversed list. i
was trying with next and iter function. but i am unable to do.

please give me solution for this basic scenario.

Thanks in Advance,
V.Sathyamoorthy

Anand Chitipothu

unread,
May 31, 2012, 1:59:56 AM5/31/12
to chen...@googlegroups.com
Looks like you are using Python3. Most Python3 built-in functions returns iterators instead of lists. You can convert it into a list using "list" function.

>>> a = [1, 2, 3, 4, 5]
>>> reversed(a)
<list_reverseiterator object at 0x1005e0510>
>>> list(reversed(a))
[5, 4, 3, 2, 1]

Or you can use list slicing to reverse a list.

>>> a[::-1]
[5, 4, 3, 2, 1]

Anand



Vishal

unread,
May 31, 2012, 2:01:11 AM5/31/12
to chen...@googlegroups.com
On Mon, May 28, 2012 at 9:28 PM, sathyamoorthy v <sathyam...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Chennaipy" group.
Wiki at http://nrcfosshelpline.in/chennaipy/
To post to this group, send email to chen...@googlegroups.com
To unsubscribe from this group, send email to chennaipy-...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/chennaipy?hl=en

hi,

reversed() returns an "iterator" that can only move along the iterable (read List) in reverse order. 
creating an iterator that moves in a certain fashion is much cheaper than actually creating a new list with the elements from the old list in the reverse order.

do this:

a = [1,2,3,4,5,6]
ra = reversed(a)
for i in ra:
    print i

# there is also a list.reverse() method that reverses the original lista.reverse()
for m in a:
    print m

--
Thanks and best regards,
Vishal Sapre

---
"Life is 10% how you make it, and 90% how you take it"
"बहुजन हिताय, बहुजन सुखाय (Benefit for most people, Happiness for most people.)"
---
Please DONT print this email, unless you really need to.
Save Energy & Paper. Save the Earth.

Anand Chitipothu

unread,
May 31, 2012, 2:07:55 AM5/31/12
to chen...@googlegroups.com

Looks like you are using Python3. Most Python3 built-in functions returns iterators instead of lists. You can convert it into a list using "list" function.

The reversed function returns an iterator even in python2.x. I thought only Python3.x does it.

Anand

B.k.Rajiv

unread,
May 31, 2012, 1:59:26 AM5/31/12
to chen...@googlegroups.com
instead of 
>>> a = reversed(a)

Try 
>>> a = list( reversed(a) )

vivek soundrapandi

unread,
Jun 1, 2012, 12:09:29 PM6/1/12
to chen...@googlegroups.com
This will reverse the list inplace,
li=[1,2,3,4]
li.reverse()

Thanks,
Vivek Soundrapandi
Reply all
Reply to author
Forward
0 new messages