Python - unpack lists and join string

I'm new to Python, but I have experience with Perl coding.

I wonder if there is an elegant way to "concatenate" lists to a string, such as the following Perl example in Python.

my $brands = [ qw( google apple intel qualcomm ) ]; #1st List  
my $otherBrands = [ qw( nike reebok puma ) ]; #2nd List  

say join ':' , ( @{$brands} , @{$otherBrands} );  
# print "google:apple:intel:qualcomm:nike:reebok:puma"

      

I've been looking for this for a while, but most of the answer uses * to unpack, which is not possible in my case.

+3


source to share


2 answers


You can use +

to combine two lists and join

to join them.

brands = ["google", "apple", "intel", "qualcomm"]
otherBrands = ["nike", "reebok", "puma"]

print ":".join(brands + otherBrands)

      



If you are looking for a syntax similar to qw

Perl (for creating a list of string literals without quotes), this does not exist in Python as far as I know.

+5


source


You can try the following

list1 = ['google', 'apple', 'intel', 'qualcomm']
list2 = ['nike', 'reebok', 'puma']
your_string = ":".join(list1+list2)

      



The way out should be

'google:apple:intel:qualcomm:nike:reebok:puma'

      

+2


source







All Articles