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

    Join Date
    Apr 2004
    Posts
    4
    Rep Power
    0

    How to remove spaces from Lists?


    Hello,

    If I type [1,2] into Python, it'll return [1, 2].

    How to make it return [1,2], without that space?

    I need to convert my results into strings (for example, str([1,2])), but I don't want extra spaces. Of course, I have a small function that may delete the spaces, but when my result is a very-very long list, and it is then converted to a string, the functoion loses a lot of time just trying to strip the string from the unnecessary spaces!

    I'd like to somehow generate the List without spaces from the very beginning.

    Does anybody know an answer to this?

    Thank you.

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

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    This is one way:
    Code:
    >>> seq = [1, 2, 3]
    >>> print "[%s]" % ",".join([str(i) for i in seq])
    '[1,2,3]'
    This is another way:
    Code:
    >>> seq = [1, 2, 3]
    >>> print str(seq).replace(" ", "")
    '[1,2,3]'
    Last edited by percivall; June 9th, 2004 at 02:12 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    4
    Rep Power
    0
    Thanks, percivall,

    That may help. My function that removes spaces, does so selectively -- that is, removing them from the lists and leavig them in some other places. I wonder if using either of the two lines you're suggesting will result in a faster overall evaluation.

    But I am still intrigued if it is possible to redefine Python standard output somehow, so that no special operations are needed to remove spaces in Lists.

    Arteum
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    I'm not sure if you understand or not that there're actually no spaces in a list. There are spaces in the representation of a list. When you give the list to str() or to repr() those functions call the __str__ respectively __repr__ methods of the list object. What those two methods do is equivalent to:
    Code:
    ", ".join([str(item) for item in seq])
    You can easily change this by deriving your own class from list and overriding the __str__ and/or __repr__ methods.
    Code:
    class mylist(list):
        def __str__(self):
            return "[%s]" % ",".join([str(item) for item in self])
        __repr__ = __str__
    But when you work with a list the representation of the list has nothing to do with the list.

    Also, FYI, the representation you see when you enter [1, 2] in the Python interpreter is the same as given by repr(). When you print a list the representation given is the same as given by str().
    Last edited by percivall; June 9th, 2004 at 04:17 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    4
    Rep Power
    0
    Thank you. That clarified everything.

IMN logo majestic logo threadwatch logo seochat tools logo