[Tutor] Short Version, New guy looking for some tips, Revisited.

Nick Lunt nick at javacat.f2s.com
Sun Feb 8 09:43:43 EST 2004


Hi,

if you saw my script on gentoo forums a couple of months ago then that was when I first touched python, so yes the script is overkill for what it does, as I've explained to Joshua already.

Your function -
> def gen_list(somedir):
>     l = os.listdir(somedir).sort()
>     return l

when I try that from the python interpreter it returns None. Im not sure why that is however.

Also this does the same:
> def list_from_dict(dict):
>     tempkeys = dict.keys().sort()
>     return tempkeys

So maybe someone on the list can tell us why appending the sort method after creation of a tuple does not work as expected.

Also Thomas there are other ways to create a dict that I did not know about at the time, such as -

mydict = dict(zip(list1, list2))

which I would of used if I'd known about it at the time of writing the program.

In fact on the gentoo forums one python programmer did the whole program in about 3 lines using 'dict' and 'lambda' ;) 

Cheers
Nick



On Sun, 08 Feb 2004 13:42:31 +0100
drstrange <strangedr at gentoo.org> wrote:

> Nick Lunt wrote:
> 
>  >Hi Joshua,
>  >
>  >I'm also a beginner at python, but I know what your saying.
>  >Also I'm working on a small compression/archive program and the list 
> helped me out with that.
> 
> 
> Hello Nick,
> 
> I am also new to Python, programming in gneral, and this list (hello, 
> list...).
> I'd like to comment/ask on your script (I believe I saw this on 
> forums.gentoo.org also). Specifically these two functions:
> def gen_list(somedir):
>     l = []
>     for arg in os.listdir(somedir):
>         l.append(arg)
>     l.sort()
>     return l
> 
> and
> 
> def list_from_dict(dict):
>     tempkeys = []
>     for key in dict.keys():
>         tempkeys.append(key)
>     tempkeys.sort()
>     return tempkeys
> 
> are overkill, I think. If I know correctly, os.listdir() returns a list, 
> and dict.keys() also returns a list, so IMHO there is no need to iterate 
> over them to produce another list. Something like this would be enough:
> 
> def gen_list(somedir):
>     l = os.listdir(somedir).sort()
>     return l
> 
> and
> 
> def list_from_dict(dict):
>     tempkeys = dict.keys().sort()
>     return tempkeys
> 
> Am I correct in this?
> 
> cheers,
> 
> Thomas Ferencz



More information about the Tutor mailing list