Warning: UPDATE command denied to user 'decalaged'@'10.0.75.62' for table 'cache' query: UPDATE cache SET data = 'a:180:{s:13:\"theme_default\";s:7:\"garland\";s:13:\"filter_html_1\";i:1;s:18:\"node_options_forum\";a:1:{i:0;s:6:\"status\";}s:27:\"menu_secondary_links_source\";s:13:\"primary-links\";s:20:\"freelinking_nodetype\";s:4:\"book\";s:15:\"install_profile\";s:7:\"default\";s:17:\"node_options_page\";a:2:{i:0;s:6:\"status\";i:1;s:7:\"promote\";}s:12:\"comment_page\";s:1:\"2\";s:18:\"drupal_private_key\";s:64:\"5ee87c350e7817c1b567a72fe25b8f8100c218a6f384611866c69facc5d9dd5a\";s:19:\"file_directory_temp\";s:4:\"/tmp\";s:13:\"user_register\";s:1:\"0\";s:9:\"clean_url\";s:1:\"1\";s:21:\"filter_default_format\";i:1;s:19:\"filter_u in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:1023:\"UPDATE command denied to user 'decalaged'@'10.0.75.62' for table 'cache_filter'\nquery: UPDATE cache_filter SET data = '<p>There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.</p>\\n<p>First, if it is a list of strings, you may simply use join this way:</p>\\n<pre>\\n>>> mylist = [\\'spam\\', \\'ham\\', \\'eggs\\']\\n&am in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:616:\"UPDATE command denied to user 'decalaged'@'10.0.75.62' for table 'cache_filter'\nquery: UPDATE cache_filter SET data = '<p>if list contains integer values, error will occur. therefore first need to convert them all into string<br />\\nexample<br />\\nint_arr = range[10]<br />\\nconvert_first_to_generator = (str(w) for w in int_arr)<br />\\nprint \\'\\'.join(convert_first_to_generator)</p>\\n', created = 1369411469, expire = 13694 in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:7:\"warning\";s:8:\"%message\";s:97:\"array_map() [<a href=\'function.array-map\'>function.array-map</a>]: Argument #2 should be an array\";s:5:\"%file\";s:59:\"/homez.14/decalaged/www/drupal/modules/system/system.module\";s:5:\"%line\";i:1015;}', 3, '', 'http://www.decalage.info/en/python/print_list', '', '54.224.75.101', 1369411469) in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:7:\"warning\";s:8:\"%message\";s:107:\"array_keys() [<a href=\'function.array-keys\'>function.array-keys</a>]: The first argument should be an array\";s:5:\"%file\";s:49:\"/homez.14/decalaged/www/drupal/includes/theme.inc\";s:5:\"%line\";i:1817;}', 3, '', 'http://www.decalage.info/en/python/print_list', '', '54.224.75.101', 1369411469) in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:7:\"warning\";s:8:\"%message\";s:39:\"Invalid argument supplied for foreach()\";s:5:\"%file\";s:49:\"/homez.14/decalaged/www/drupal/includes/theme.inc\";s:5:\"%line\";i:1817;}', 3, '', 'http://www.decalage.info/en/python/print_list', '', '54.224.75.101', 1369411469) in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128
Python tips - How to easily convert a list to a string for display | Decalage

Python tips - How to easily convert a list to a string for display

  • user warning: UPDATE command denied to user 'decalaged'@'10.0.75.62' for table 'cache_filter' query: UPDATE cache_filter SET data = '<p>There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.</p>\n<p>First, if it is a list of strings, you may simply use join this way:</p>\n<pre>\n&gt;&gt;&gt; mylist = [\'spam\', \'ham\', \'eggs\']\n&gt;&gt;&gt; print <strong>\', \'.join</strong>(mylist)\nspam, ham, eggs\n</pre><p>Using the same method, you might also do this:</p>\n<pre>\n&gt;&gt;&gt; print <strong>\'\\n\'.join</strong>(mylist)\nspam\nham\neggs\n</pre><p>However, this simple method does not work if the list contains non-string objects, such as integers.</p>\n<p>If you just want to obtain a comma-separated string, you may use this shortcut:< in /homez.14/decalaged/www/drupal/includes/cache.inc on line 109.
  • user warning: UPDATE command denied to user 'decalaged'@'10.0.75.62' for table 'cache_filter' query: UPDATE cache_filter SET data = '<p>if list contains integer values, error will occur. therefore first need to convert them all into string<br />\nexample<br />\nint_arr = range[10]<br />\nconvert_first_to_generator = (str(w) for w in int_arr)<br />\nprint \'\'.join(convert_first_to_generator)</p>\n', created = 1369411469, expire = 1369497869, headers = '', serialized = 0 WHERE cid = '1:b30308f584b02b286af8761945947117' in /homez.14/decalaged/www/drupal/includes/cache.inc on line 109.
  • warning: array_map() [function.array-map]: Argument #2 should be an array in /homez.14/decalaged/www/drupal/modules/system/system.module on line 1015.
  • warning: array_keys() [function.array-keys]: The first argument should be an array in /homez.14/decalaged/www/drupal/includes/theme.inc on line 1817.
  • warning: Invalid argument supplied for foreach() in /homez.14/decalaged/www/drupal/includes/theme.inc on line 1817.

There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.

First, if it is a list of strings, you may simply use join this way:

>>> mylist = ['spam', 'ham', 'eggs']
>>> print ', '.join(mylist)
spam, ham, eggs

Using the same method, you might also do this:

>>> print '\n'.join(mylist)
spam
ham
eggs

However, this simple method does not work if the list contains non-string objects, such as integers.

If you just want to obtain a comma-separated string, you may use this shortcut:

>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip('[]')
80, 443, 8080, 8081

Or this one, if your objects contain square brackets:

>>> print str(list_of_ints)[1:-1]
80, 443, 8080, 8081

Finally, you may use map() to convert each item in the list to a string, and then join them:

>>> print ', '.join(map(str, list_of_ints))
80, 443, 8080, 8081
>>> print '\n'.join(map(str, list_of_ints))
80
443
8080
8081

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

if list contains integer

if list contains integer values, error will occur. therefore first need to convert them all into string
example
int_arr = range[10]
convert_first_to_generator = (str(w) for w in int_arr)
print ''.join(convert_first_to_generator)


Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:0:\"\";s:5:\"%file\";s:49:\"/homez.14/decalaged/www/drupal/includes/cache.inc\";s:5:\"%line\";i:109;}', 3, '', 'http://www.decalage.info/en/python/print_list', '', '54.224.75.101', 1369411469) in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:232:\"UPDATE command denied to user &#039;decalaged&#039;@&#039;10.0.75.62&#039; for table &#039;node_counter&#039;\nquery: UPDATE node_counter SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = 1369411469 WHERE nid = 45\";s:5:\"%file\";s:67:\"/homez.14/decalaged/www/drupal/modules/statistics/statistics.module\";s:5:\"%line\";i:54;}', 3, '', 'http://www.decalage.info/en/python/print_list', '', '54.224.75.101', 1369411469) in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:407:\"INSERT command denied to user &#039;decalaged&#039;@&#039;10.0.75.62&#039; for table &#039;accesslog&#039;\nquery: INSERT INTO accesslog (title, path, url, hostname, uid, sid, timer, timestamp) values(&#039;Python tips - How to easily convert a list to a string for display&#039;, &#039;node/45&#039;, &#039;&#039;, &#039;54.224.75.101&#039;, 0, &#039;664a675ccb3a304d06444cd17e318192&#039;, 217, 1369411469)\";s:5:\"%file\";s:67:\"/homez.14/decalaged/www/drupal/modules/statistics/statistics.module\";s:5:\"%line\ in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128

Warning: INSERT command denied to user 'decalaged'@'10.0.75.62' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:1023:\"UPDATE command denied to user &#039;decalaged&#039;@&#039;10.0.75.62&#039; for table &#039;sessions&#039;\nquery: UPDATE sessions SET uid = 0, cache = 0, hostname = &#039;54.224.75.101&#039;, session = &#039;messages|a:1:{s:5:\\&quot;error\\&quot;;a:3:{i:0;s:80:\\&quot;user warning: in /homez.14/decalaged/www/drupal/includes/cache.inc on line 109.\\&quot;;i:1;s:329:\\&quot;user warning: UPDATE command denied to user &amp;#039;decalaged&amp;#039;@&amp;#039;10.0.75.62&amp;#039; for table &amp;#039;node_counter&amp;#039;\\n in /homez.14/decalaged/www/drupal/includes/database.mysql.inc on line 128