Returning a List

Larry Hudson orgnut at yahoo.com
Sat Oct 4 04:21:27 CEST 2014


On 10/03/2014 04:35 AM, Shiva wrote:
> Hi All,
>
> I might be doing something really silly here, but I can't seem to spot it:
>
> def front_x(words):
>    b=[]
>    c=[]
>    for a in words:
>       if a[0] == 'x':
>           b.append(a)
>       else:
>           c.append(a)
>
>    b = sorted(b)
>    c = sorted(c)
>    d= b+c
>    print('d = ',d)
>
>    #return b+c
>    return d
>
> front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa'])
>
>
>
> Why is return d or return b+c not returning anything??
>
> The d's value is confirmed by the print statement.
>
> Thanks,
> Shiva.
>
Works fine for me...

For a quickie test, I copy/pasted this code into Idle, and it works.

I suspect you are trying to run this as a script -- but remember, when running interactively 
(directly in Python or in Idle) the return values are displayed automatically.  This is NOT true 
when running as a program, you have to specifically print the return values (or save them in a 
variable to access them later).  Change your calling line to:

print(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']))

      -=- Larry -=-




More information about the Python-list mailing list