Your function prints your string to stdout, then returns the list itself unchanged.
Build a string and return that instead of printing:
def to_string(my_list):
result = ''
last = len(my_list) - 1
for pos, elem in enumerate(my_list):
result += str(elem)
if pos != last:
result += ', '
return result
This loops over all elements, keeping a position counter with the enumerate() function; this way it's easy to detect if we are adding the last element to the result.
Your function prints your string to stdout, then returns the list itself unchanged.
Build a string and return that instead of printing:
def to_string(my_list):
result = ''
last = len(my_list) - 1
for pos, elem in enumerate(my_list):
result += str(elem)
if pos != last:
result += ', '
return result
This loops over all elements, keeping a position counter with the enumerate() function; this way it's easy to detect if we are adding the last element to the result.
You could also use reduce function:
aList=['w','o','r','d']
aString=reduce(lambda x,y:x+y,aList)
python - How to convert list to string - Stack Overflow
python - Convert a list to string without commas - Stack Overflow
Converting a lists of characters into a list of strings
how to convert a parameter list into a string in python without using the join function - Stack Overflow
but I am unable to print it as a string
Just unpack the arguments out of the list using the * operator when calling print() and use "" as a separator:
>>> L = ['g', 'i', 'n', 'o', 'r', 't', 'S', '1', '3', '2', '4']
>>> print(*L, sep="")
ginortS1324
You can use reduce, it is not in forbidden list. Append this line at the end of your code:
new_res=reduce( lambda x,y: x+y, res, "")
print(new_res)
','.join(f'{x} {y}' for (x,y) in l)
You have two processes you need to combine. First you need to unpack each pair of co-ordinates into a string like 0 1 for [0, 1] this could be done using a join but, IMO, it's easier and clearer to simply used a formatted string, that's the f'{x} {y}' which replaces the {x} and {y} with the value of the variable x and y.
Second, you need to read these pairs out of the containing list l, this can be done using a comprehension. In your case you want to unpack as you go which you can do with for (x, y) in l. You can then combine these with a join to introduce the commas between co-ordinates that you want.
So, including your encasing parentheses we end up with a function definition of:
def conv(l):
return f"(({','.join(f'{x} {y}' for (x,y) in l)}))"
You need to join the inner lists with a space and the outer list with a comma:
l = [[0,0], [1,0], [1,1], [0,1], [0,0]]
s = ','.join(' '.join(map(str, sub)) for sub in l)
print(f"(({s}))")
Will give:
((0 0,1 0,1 1,0 1,0 0))
This seems like it has a simple solution but I can't seem to find it. I have a list
test = [1, 2, 3, 4]
and I want to convert it to the string and remove the brackets and spaces
1,2,3,4
I've found that I can print the list correctly by using
print(*test, sep=',')
but I want to concatenate the string for use elsewhere, not just print it.
Any ideas?