Results 1 to 6 of 6

Thread: Making lists in python

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    Making lists in python

    Isn't there a way to split items in a list into text separated by spaces? Like turn

    [4, 6, 9]
    INTO
    4 6 9

  2. #2
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Making lists in python

    I'm confident that there's a much more efficient way, but for now (while I research it):

    Code:
    #any list here
    some_list = [1, 2, 3]
    
    #blank storage string
    some_list_string = ''
    
    #iterate over every item in the list
    for num in some_list:
         #append the number and a space
         some_list_string += str(num) + ' '
    
    print some_list_string
    After some research, it turns out that it's quite easy to just convert the list into a string (as python's print does), then remove extraneous characters. E.g.

    Code:
    #Any list here.
    some_list = [1, 2, 3]
    
    #Convert to string, remove '[' and ']', and remove commas.
    some_list_string = str(some_list).strip('[]').replace(',', '')
    
    print some_list_string
    EDIT 2: Python's join is even better, see the people below.
    Last edited by MG&TL; March 6th, 2013 at 03:42 PM.

  3. #3
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Making lists in python

    I think the Python way would be:

    Code:
    ' '.join(some_list)
    EDIT: Or, if it's a list of integers:

    Code:
    ' '.join([str(n) for n in some_list])
    Last edited by r-senior; March 6th, 2013 at 03:47 PM. Reason: Ah, Vaphell beat me too it
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Making lists in python

    integers need to be converted to strings with str()
    Code:
    $ python
    Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
    [GCC 4.6.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> l=[4,6,9]
    >>> ' '.join(str(x) for x in l)
    '4 6 9'
    Last edited by Vaphell; March 6th, 2013 at 03:44 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jan 2012
    Beans
    161

    Re: Making lists in python

    Thanks guys for the help

  6. #6
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Making lists in python

    Code:
    ' '.join(map(str, list))
    should work, too.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •