Hi all,

I am a Python Newbie and started learning it with this book "Programming in Python 3", which has a lot of sample codes.
So I am using Python 3 and got stuck while trying this program.The program is to swap the elements in the Tuple and how to do it by using a function object.Sorted method is also used to sort the result.

def swap(t):
    return t[1],t[0]

x=zip((1,3,1,3),("param","dorie","kayak","canoe"))

y=sorted(x,key=swap)
print('y=',y)

z=sorted(x)
print('z=',z)

l=sorted(x,key=swap)
print('l=',l)

While executing this program Python 3 gives an empty list for 'z' and 'l'.So i tried executing this program using Python 2.6 and got a different result which I assume is correct.These are the outputs

----------Python 3----------

C:\Python30\ExPython>c:\Python30\python.exe ExPFI.py
y= [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')]
z= []
l= []

----------Python2.6----------

C:\Python30\ExPython>c:\Python26\python.exe ExPFI.py
('y=', [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')])
('z=', [(1, 'kayak'), (1, 'param'), (3, 'canoe'), (3, 'dorie')])
('l=', [(3, 'canoe'), (3, 'dorie'), (1, 'kayak'), (1, 'param')])

Can you please help me by explaining ,why 'z' and 'l' is getting the empty strings while running program with Python 3.

Thanks in Advance

In Python 3.0 the built-in method zip() contains an iterable instead of a list (much like the new range() function)... meaning that after the first sorted() call, the internal pointer of the zip object is pointed at the END position of the object.

I don't think it's possible to "reset" the iterator without simply re-zipping the two lists....
HTH

Or, convert the zip to a list.

def swap(t):
    return t[1],t[0]

x=zip((1,3,1,3),("param","dorie","kayak","canoe"))

w=list(x)
print("w=", w)

y=sorted(w,key=swap)
print('y=',y)

z=sorted(w)
print('z=',z)

l=sorted(w,key=swap)
print('l=',l)

Does your book "Programming in Python 3" refer to Python version 3.0 or is it simply the third edition of the book? If it refers to version 3.0+ then the sample code is buggy!

Looks like sample code is buggy:

Product Description

Python 3 is the best version of the language yet: It is more powerful, convenient, consistent, and expressive than ever before. Now, leading Python programmer Mark Summerfield demonstrates how to write code that takes full advantage of Python 3’s features and idioms. The first book written from a completely “Python 3” viewpoint, Programming in Python 3 brings together all the knowledge you need to write any program, use any standard or third-party Python 3 library, and create new library modules of your own.

Source

Jim ,woooee,sneekula

Thanks a Mil for your help..

sneekula
Programmming in Python3 is the first book on Python3000 in the market.It's a great book.
The program which I posted is not a sample code from the text.All the sample codes in the book which I tried until now is working properly.

I got this bug when i modified a one of the examples in the book ,to try different things.

regards

pvi101, welcome to the forum.
Looks like this might be a good book after all!
Hope to see you back soon asking the kind of questions like this one. We will all try to help and learn in the process with you.

pvi101, welcome to the forum.
Looks like this might be a good book after all!
Hope to see you back soon asking the kind of questions like this one. We will all try to help and learn in the process with you.

Thank You and I will,
Thinking about this error again for sometime.I am wondering whether ,the behaviour of zip object in both Python3 and Python2.6 should be the same?

if the internal pointer is not moving after the first sorted call to zip object ,that is kind of a bug in the python3 itself?Am i right on this.

Regards

This was a change from python 2.X to 3.0 to provide better performance.

In Python 2.X versions, zip() returned a list:

>>> zip(['Blue', 'Bunny', 'Red'],['1','2','3'])
[('Blue', '1'), ('Bunny', '2'), ('Red', '3')]
>>>

However for the sake of saving time, in 3.0 they decided to change the behavior so that zip() returned an iterator instead. The same thing happened with range()... if you type range(5) into 3.0 you'll get an iterator; yet 2.X gives this:

>>> range(5)
[0, 1, 2, 3, 4]
>>>

Try it!
**Note: range() in Python 3.0 is actually xrange() from python 2.X**

This was a change from python 2.X to 3.0 to provide better performance.

In Python 2.X versions, zip() returned a list:

>>> zip(['Blue', 'Bunny', 'Red'],['1','2','3'])
[('Blue', '1'), ('Bunny', '2'), ('Red', '3')]
>>>

However for the sake of saving time, in 3.0 they decided to change the behavior so that zip() returned an iterator instead. The same thing happened with range()... if you type range(5) into 3.0 you'll get an iterator; yet 2.X gives this:

>>> range(5)
[0, 1, 2, 3, 4]
>>>

Try it!
**Note: range() in Python 3.0 is actually xrange() from python 2.X**

Many Thanks jlm699

This article has been dead for over six months. Start a new discussion instead.