About python while statement and pop()
Chris Angelico
rosuav at gmail.com
Wed Jun 11 23:58:27 EDT 2014
On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre
<vincent.vandevyvre at swing.be> wrote:
> Le 12/06/2014 05:12, hito koto a écrit :
>
>> Hello,all
>> I'm first time,
>>
>> I want to make a while statement which can function the same x.pop () and
>> without the use of pop、how can i to do?
>>
>> i want to change this is code:
>>
>> def foo(x):
>> y = []
>> while x !=[]:
>> y.append(x.pop())
>> return y
>
> Something like that :
>
> def foo(x):
> return reversed(x)
That doesn't do the same thing, though. Given a list x, the original
function will empty that list and return a new list in reverse order,
but yours will return a reversed iterator over the original list
without changing it. This is more accurate, but still not identical,
and probably not what the OP's teacher is looking for:
def foo(x):
y = x[::-1]
x[:] = []
return y
If the mutation of x is unimportant, it can simply be:
def foo(x):
return x[::-1]
ChrisA
More information about the Python-list
mailing list