Python Forum
Join 2 items of array without a separator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Join 2 items of array without a separator
#1
Hi all
Im trying to join 2 items together from a 2D array without a separator

x = [[2, "D"], [6, "H"], [8, "S"], [5, "H"]]
print(x[2])

print(x[2][0])
print(x[2][1])
Im wanting the output to be assigned to a variable for printing, but it comes out as
[8, 'S']
8
S
what I want is 8S as the new variable to print.
Im sure im being dim hope someone can help TIA

Jai
Reply
#2
print('{}{}'.format(*x))
The * operator turns the list x into two positional parameters to format. The format method replaces the curly braces with the string versions of the parameters. The format method allows for lots of formatting of the data you turn into a string, as well. Check out the docs.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
my_list = [[2, "D"], [6, "H"], [8, "S"], [5, "H"]]
for item in my_list:
    print(''.join(map(str, item)))

for item in my_list:
    print('{}{}'.format(*item))

# for 3.6+ python
for item1, item2 in my_list:
    print(f'{item1}{item2}')
the last one is only if you use python 3.6+
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Great thank you Admin
Great as well thanks buran.
Now to mark as SOLVED
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 483 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Printing a raw string with a folder separator at the end, duplicates the separator krulah 5 1,220 Nov-28-2022, 12:41 PM
Last Post: snippsat
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,438 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Numpy savetxt, how save number with decimal separator SpongeB0B 1 3,210 May-10-2020, 01:05 PM
Last Post: ThomasL
  Loop through array items dynamically in a certain format bhojendra 3 2,642 Jun-11-2019, 03:37 AM
Last Post: micseydel
  Question about doc strings and tuple separator ? mcgrim 1 1,908 Mar-20-2019, 12:57 PM
Last Post: Yoriz
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,233 Feb-14-2019, 08:34 PM
Last Post: woooee
  split string and keep separator wardancer84 2 2,968 Nov-21-2018, 11:20 AM
Last Post: wardancer84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020