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

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Iterate over a list of strings.


    lets say a have this list:

    Code:
    list['565', '095', '4243','3003','23D5']
    is it possible to iterate over this list of strings with "str.isdigit()" function somehow?
    or the only way is to combine all the string in the list to one big string and then use "str.isdigit()"?

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

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    2
    The shortest code to do this is exactly how say, join the list to a single string and check the string
    Code:
    "".join(['565', '095', '4243','3003','23D5']).isdigit()
    As your question appears to be looking for alternatives, you can iterate over a list with the for loop. You can then check each item in the list contains digits in the usual way. If you and the result cumlatively this will give you the answer you want.

    This makes a nice little function you can call or expand on:
    Code:
    def islistdigits ( inlist ):
        "inlist must contain only strings - checks each item in the list contains only digits"
        listalldigits = True # default in case of an empty list
        for listitem in inlist:
            listalldigits = listalldigits and listitem.isdigits()
        return listalldigits
    If you are looking for a fancy way of doing it then the reduce function is what you are looking for but you will need to define the comparison function first

    Code:
    def myand ( x , y ):
        return (x or x.isdigit()) and y.isdigit()
    reduce(myand,['565', '095', '4243','3003','23D5'])
    If you are using Python 3 you will need to use functools.reduce

    As an added bonus, if you wanted to reduce the list to contain only those strings with just digits in them you could use the filter function as follows:
    Code:
    def onlydigits ( x ):
        return x.isdigit()
    filter(onlydigits,['565', '095', '4243','3003','23D5'])
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Hi, Quackajack.

    Originally Posted by Quackajack
    The shortest code to do this is exactly how say, join the list to a single string and check the string
    Code:
    "".join(['565', '095', '4243','3003','23D5']).isdigit()
    i will, of course, prefer the shrortest code.
    thanks for the help!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Well, the following’s not particularly long either:

    Code:
    all(n.isdigit() for n in ['565', '095', '4243','3003','23D5'])
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    2
    I learn something new every day. I wasn't aware of the all function
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Thanks guys!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    all, any, divmod. Useful functions.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo