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

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Concantenating lists?


    Noob here. I'm looking for way to add all the strings from one list to each of the strings from another. For example,
    x =['one','two','three']
    y=['1','2','3']

    I want to end up with a list that adds y values to each x value.
    z=['one1','one2','one3','two1','two2',.....]
    Then when this list is achieved I'm interested on ways to write it to a text file. thank you for any help here.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Use list comprehensions:
    Code:
    z = [i + j for i in x for j in y]
    Equivalent of:
    Code:
    z = []
    for i in x:
        for j in y:
            z.append(i + j)
    Read about file handles if you want to write to a file.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481

    use zip


    use zip. zip works approximately like transpose.


    Code:
    >>> x =['one','two','three']
    >>> y=['1','2','3']
    >>> 
    >>> [''.join(a) for a in zip(x,y)]
    ['one1', 'two2', 'three3']
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Whoops! Can't retract disagreements.
    I blew it. Sorry.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    487
    Rep Power
    33
    You were possibly thinking of product from itertools, which does what is desired but returns tuples so the elements would have to be joined.
    Code:
    import itertools
    
    x =['one','two','three']
    y=['1','2','3']
    product_list = list(itertools.product(x, y))
    print product_list
    print [''.join(a) for a in product_list]

    Comments on this post

    • codergeek42 agrees : itertools saves the day AGAIN! I love that module...
    • noobie1000 disagrees : C'mon, why stress python and go through the trouble of importing itertools when a simple list comprehension would suffice for this job!!
    Last edited by dwblas; November 7th, 2013 at 11:34 AM.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    That's charitable. Yes, Cartesian product answers solves JonnyDoeInWisco's problem.

    Maybe it's time for me to quit this "job".
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Talking


    Perfect! Thank you very much guys

IMN logo majestic logo threadwatch logo seochat tools logo