I am new to Python, but I have experience in Perl coding.
I wonder if there is an elegant way to 'join' lists into a string like 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 searched this for a while, but most of the answer are using * for unpacking which is not feasible in my case.
You can use +
to join 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 syntax similar to qw
in Perl (to create a list of string literals without quotes), that does not exist in Python as far as I know.
You can try the following
list1 = ['google', 'apple', 'intel', 'qualcomm']
list2 = ['nike', 'reebok', 'puma']
your_string = ":".join(list1+list2)
The output should be
'google:apple:intel:qualcomm:nike:reebok:puma'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With