-1

I have a list

my_list = [[200.0, 10.0], [250.0, 190.0], [160.0, 210.0]]

I want get the list of these coordinate with space between them

req_list = "200,10 250,190 160,210"

to write these in SVG format for polygons.

I tried replacing "[]" with " " but replace doesn't work for an array

my_list.replace("[", " ")
user110920
  • 57
  • 4
  • 1
    "I tried replacing "[]" with " " but replace doesn't work for an array" Yes, this approach is nonsense because the list (we don't call them arrays) **does not contain** the `[` symbols etc. It is essential to understand what data types are, and how they work, in order to write sensible code. The approach is simple, and this is a common problem combining elementary techniques shown in the linked duplicates. Each sub-list needs to be joined up as a string, and then those strings need to be joined using the same technique. – Karl Knechtel Nov 18 '22 at 22:15

2 Answers2

1

You can use str.join to the sublists:

my_list = [[200.0, 10.0], [250.0, 190.0], [160.0, 210.0]]

req_list = " ".join(",".join(f"{int(v)}" for v in l) for l in my_list)
print(req_list)

Prints:

200,10 250,190 160,210
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

You can iterate through the list and append them into an empty string defined, for example:

req_list = ""
for cor in my_list:
    req_list += '{},{} '.format(int(cor[0]),int(cor[1]))
print(req_list[:-1])

Prints:

200,10 250,190 160,210

Indexed till -1 is to ignore the last white space.

VIM
  • 38
  • 6