#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    4
    Rep Power
    0

    Stupid noob question - find # elements in list/dictionary?


    I've looked through the tutorial and library references, and have searched google. So I'm sure I've just overlooked it, but is there any way to get the number of elements in a list and dictionary (guess it would apply to tuples as well).

    For now the only way I know how is something like:

    count = 0
    fox x in containername:
    count += 1

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    len() seems to be what you want:
    Code:
    >>> lst = [1,2,3]
    >>> len(lst)
    3
    >>> d = {'a':1, 'b':2}
    >>> len(d)
    2
    Regards, mawe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    4
    Rep Power
    0
    Originally Posted by mawe
    Hi!

    len() seems to be what you want:
    Code:
    >>> lst = [1,2,3]
    >>> len(lst)
    3
    >>> d = {'a':1, 'b':2}
    >>> len(d)
    2
    Regards, mawe
    lol.. figured it was simple. I looked and looked but no love. Will look through the built-ins next time as well. I'm having a hard time figuring out where to go to find documentation as the documentation seems kind of random. Anyways Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Pythons documentation is acutally very nice. It can be a little bit of work to get to know how to use it. But once you do you will use it for almost everything.

    for general syntax and help on standard stuff the python tutorial is nice: http://docs.python.org/tut/tut.html

    for a libary referance python's web site also has some good stuff:
    http://docs.python.org/lib/lib.html

    but google is also a good source
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    4
    Rep Power
    0
    Hope you're right. I did try to use the tutorial, the library reference, and I have OReilly's Python in a Nutshell. A lot of it is probably my noobness. I find it's a little easier to find things now that I've been working with Python for a week or so now, but I've searched several times. That's why I had to ask this stupid question on this forum, as the info was in the built-ins category (ie how is a noob supposed to know that?).

    Anyways thanks!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    i guess there isn't exactly a way for a newb to know that. I suppose it just comes with experance.

    If it is used a lot and is able to be used for multible things that are diff but share that same attribute then there is probably a common method of getting it. I suppose is a good way to think of it.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Any way just a comment ot the book, I doubt that Python in a Nutshell is the right book for a noob, I'd suggest Learning Python from Mark Lutz and David Ascher
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by CyBerHigh
    Pythons documentation is acutally very nice. It can be a little bit of work to get to know how to use it. But once you do you will use it for almost everything.

    for general syntax and help on standard stuff the python tutorial is nice: http://docs.python.org/tut/tut.html

    for a libary referance python's web site also has some good stuff:
    http://docs.python.org/lib/lib.html

    but google is also a good source
    Maybe somone would like to add a short post on how to use the docs, what sections are of interest etc. to the Tips thread at the top of the forum. I think that mighe make things easier .

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    4
    Rep Power
    0
    That is a good suggestion nettyan.

    A lot of the time I end up having to guess at the name of a command, and use the index, which has worked a few times. I think the tutorial is great for what it is, and the library reference seems to be decent only if you know EXACTLY where to look.

    I hope I don't sound like I'm dissing the docs just because I'm new to all of this, but I've been left high and dry a few times searching for things like how to work with hex in python, how to find out if a file is a file. So hoping not to sound like I'm totally crazy and stupid, I want to give an example of where I've found problems. (have used both the HTML version and windows CHM version of Python 2.4 docs)

    One example. If you want to find out whether a filename is a file that exists, I go to the library reference. Ok, there are two sections - generic and optional. That organization, to me, seems like it's there for the sake of python implementation developers -- not for developers trying to find API information. How am I as a developer supposed to know what is going to be optional, and what is generic (except through experience)? Either way, I look through both. Section 7 - the optional stuff is mostly for what seems to be a mishmash of unix-related tools, threading, networking etc. I look through 6.1 (os) - "Miscellaneous operating system interfaces". Whoa. Things like file object creation and process management are MISCELLANEOUS? It sounds to me like they really need their own section as IMPORTANT operating system interfaces. Then I look in section 6.2 - pathname manipulations. To me it's not intuitive that I should look at "Common pathname manipulations" to find info on how to find if a file exists. There should be a large separate section on "file manipulation" or something.

    Round 2 - finding info on how python handles hex. Looked through the tutorial, found nothing. For the library reference, I try part 2 (Built-In Objects), then 2.3 -> numeric types. No useful info there. Miscellaneous -> math, nothing there. Go to the index, search for hex... find hex( ). Aahh. But it doesn't reveal anything useful. I know I could find out easily with a test program or in the interpreter, but just a simple example would help 100% more. 2.4.3 is on numeric literals, but nothing helpful.

    Overall it seems the library reference was put together, and the tutorial was written to kind of glue it together somewhat. I'll probably need a python for dumbasses book to help, but I'm just relaying my experiences with the docs so far

    I've found a lot of cool stuff by browsing the library reference, but again a lot of it seems magical in regards to organization, making it hard for me to locate information. I'm blasting things because I've been pretty frustrated so far in finding help. I really hope you guys can prove me wrong, as I'm sure I'm still pretty green to all of this. You guys have been great btw. And despite all of the hate towards the docs, I've been really loving my experience with Python. I don't really feel like I'm doing a whole lot, and yet it's getting a large amount of work done -- it's great!
  18. #10
  19. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    I agree with that. What python really needs is something like the php manual. It lists everything by what you can do, and gives examples (very important). The web version also has lots of fantastic comments / tips from other users.

    it's also split up into sections which are basically "heres what you can do with strings, oh see also x, y, z". Example - string functions.

    Originally Posted by PHP Manual
    chunk_split*--*Split a string into smaller chunks
    convert_cyr_string*--* Convert from one Cyrillic character set to another
    convert_uudecode*--* Decode a uuencoded string
    convert_uuencode*--* Uuencode a string
    count_chars*--* Return information about characters used in a string
    (where the function name is a link to the function reference).

    Another fantastic feature is the searchability of the manual:
    www.php.net/<funct. name> will search the site. If it's a correct function name, you'll get the info page for that function. If it doesn't match, then it'll show a full text search of the manual and give hints.

    I'd kill for something like this for python or even just a list of functions which do things to, say, files, and a one line description. It saves so much time when you're hacking away, and hit a block like 'how do I do x to a file'?

    I may start a project to do this when I get some time. Maybe a wiki type o' thing?

    --Simon
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Well for examples one of the greatest has to be on aspn's cookbook. It has a collection of examples of how to applie certain concepts or get certain outputs type of thing. Is pretty good. I am also creating my own cookbook that will be open to everyone it should be done in a few days or so on my site.

    but aspns cookbook is located at : http://aspn.activestate.com/ASPN/Python/Cookbook/

    also, i really think the python docs are more or less good for if you know exactly what it is called, not for looking for new things. For new things i recommend googling it. Google has done a very good job with searching the python docs so python's docs are usually on the list so it cuts on on the searching though the docs on your own.
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I agree entirely, I can remember getting very frustrated with the Python docs in the past! This is probably the main area which python is lacking in right now .

    As Simon made evident in his post search facilities make all the difference, though you can kinda get around this by using Google it would be a nice improvement .

    It would be nice to setup a communal project if possible Grim and I were planning something like this a while back but it didn't come to much sadly.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    1
    Rep Power
    0
    Originally Posted by SimonGreenhill
    I agree with that. What python really needs is something like the php manual. It lists everything by what you can do, and gives examples (very important). The web version also has lots of fantastic comments / tips from other users.

    it's also split up into sections which are basically "heres what you can do with strings, oh see also x, y, z". Example - string functions.
    I believe that what you are looking for can be found here
    Python Quick Reference

    /tune

IMN logo majestic logo threadwatch logo seochat tools logo