0

I have looked everywhere, and can't find an answer. I am about 1-2 weeks old to Python so not very long. How can I join different items in lists with a string?

In my case, I want to show the 3D dimensions of an object: (Simplified a lot)

my_list = [1, 2, 3]
join_list = my_list[0],"x",my_list[1],"x",my_list[2]
print(join_list)

This returns with:

(1, 'x', 2, 'x', 3)

I am aiming to get 1x2x3 instead of (1, 'x', 2, 'x', 3). Any ideas? Thanks!

George_E -old
  • 188
  • 1
  • 3
  • 17

2 Answers2

2

This is a simple Python str.join operation. The problem that I just realized was that you have to convert the integers in the list to strings first. You can do that with a simple list comprehension like this.

'x'.join([str(x) for x in my_list])
tdube
  • 2,453
  • 2
  • 16
  • 25
0

Try this

'x'.join(str(e) for e in my_list)
mipurale
  • 57
  • 1
  • 6