View Full Version : Making lists in python
wingnut2626
March 6th, 2013, 03:27 PM
Isn't there a way to split items in a list into text separated by spaces? Like turn
[4, 6, 9]
INTO
4 6 9
MG&TL
March 6th, 2013, 03:31 PM
I'm confident that there's a much more efficient way, but for now (while I research it):
#any list here
some_list = [1, 2, 3]
#blank storage string
some_list_string = ''
#iterate over every item in the list
for num in some_list:
#append the number and a space
some_list_string += str(num) + ' '
print some_list_string
After some research, it turns out that it's quite easy to just convert the list into a string (as python's print does), then remove extraneous characters. E.g.
#Any list here.
some_list = [1, 2, 3]
#Convert to string, remove '[' and ']', and remove commas.
some_list_string = str(some_list).strip('[]').replace(',', '')
print some_list_string
EDIT 2: Python's join is even better, see the people below.
r-senior
March 6th, 2013, 03:41 PM
I think the Python way would be:
' '.join(some_list)
EDIT: Or, if it's a list of integers:
' '.join([str(n) for n in some_list])
Vaphell
March 6th, 2013, 03:41 PM
integers need to be converted to strings with str()
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l=[4,6,9]
>>> ' '.join(str(x) for x in l)
'4 6 9'
wingnut2626
March 6th, 2013, 03:47 PM
Thanks guys for the help
schragge
March 6th, 2013, 04:52 PM
' '.join(map(str, list))should work, too.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.