How do i combine two lists into combined strings ?

This is the place for queries that don't fit in any of the other categories.

How do i combine two lists into combined strings ?

Postby phantom » Thu Jul 25, 2013 10:01 am

Ok here is my problem:

I have two arrays:

Code: Select all
1st array = ["hello" , "hello1" , "hello2]
2nd array = ["me" , "you" , "all"]


and i would like to join them on this way (if it's possible ofcourse!):

Code: Select all
hellome
hello1me
hello2me

helloyou
hello1you
hello2you

helloall
hello1all
hello2all
Last edited by Yoriz on Thu Jul 25, 2013 4:26 pm, edited 2 times in total.
Reason: Added code tags and changed title.
phantom
 
Posts: 2
Joined: Thu Jul 25, 2013 9:43 am

Re: Arrays help

Postby metulburr » Thu Jul 25, 2013 10:26 am

1) use code tags

It would be a simple nested for loop:

Code: Select all
l1 = ["hello" , "hello1" , "hello2"]
l2 = ["me" , "you" , "all"]

for j in l2:
    for i in l1:
        print(i + j)
    print()

output:
hellome
hello1me
hello2me

helloyou
hello1you
hello2you

helloall
hello1all
hello2all
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1331
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Arrays help

Postby phantom » Thu Jul 25, 2013 10:40 am

It works :D Thank you very much for help!
phantom
 
Posts: 2
Joined: Thu Jul 25, 2013 9:43 am

Re: How do i combine two lists into combined strings ?

Postby tnknepp » Fri Jul 26, 2013 1:45 pm

phantom,
Not to be overly picky, but your "arrays" are not arrays, they are lists. It's just semantics, but important to call things by their proper names to avoid future confusion. FYI ;)
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests