Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Join a list of integers

Tags:

python

I am trying to get list of numbers from:

numbers= 1,2 

to:

'1','2' 

I tried ",".join(str(n) for n in numbers) but it wont give the targeted format.

like image 616
Hussein Zawawi Avatar asked Jun 21 '12 13:06

Hussein Zawawi


People also ask

How do you join a list of numbers in Python?

Use the join() method of Python. First convert the list of integer into a list of strings( as join() works with strings only). Then, simply join them using join() method.

How do you concatenate integers in a list in Python?

The extend method() adds all the elements of the list till the end. To concatenate the list on integers “+” is used. The print(newlist) is used to get the output.

How do I print a list of numbers in a string in Python?

Use the Built-In Map Function to Print a List in Python. If you want to print a list of integers, you can use a map() function to transform them into strings. Then you can use the join() method to merge them into one string and print them out.

Can we join list in Python?

Python Join List. We can use the python string join() function for joining a list of the strings. However, this function takes iterable as an argument and the list is iterable, so we are able to use it with the list.


2 Answers

How about that?

>>> numbers=1,2 >>> numbers (1, 2) >>> map(str, numbers) ['1', '2'] >>> ",".join(map(str, numbers)) '1,2' 
like image 160
Gandi Avatar answered Sep 27 '22 20:09

Gandi


>>> numbers = 1,2 >>> print ",".join("'{0}'".format(n) for n in numbers) '1','2' 
like image 30
jamylak Avatar answered Sep 27 '22 20:09

jamylak