title: How does str(list) work?
body: <p><strong>Why does <code>str(list)</code> returns how we see list on the console? How does <code>str(list)</code> work? (any reference to the CPython code for <code>str(list)</code>)?</strong></p>
  
  <pre><code>&gt;&gt;&gt; x = ['abc', 'def', 'ghi']
  &gt;&gt;&gt; str(x)
  "['abc', 'def', 'ghi']"
  </code></pre>
  
  <p>To get the original list back from the <code>str(list)</code> I have to:</p>
  
  <pre><code>&gt;&gt;&gt; from ast import literal_eval
  &gt;&gt;&gt; x = ['abc', 'def', 'ghi']
  &gt;&gt;&gt; str(x)
  "['abc', 'def', 'ghi']"
  &gt;&gt;&gt; list(str(x))
  ['[', "'", 'a', 'b', 'c', "'", ',', ' ', "'", 'd', 'e', 'f', "'", ',', ' ', "'", 'g', 'h', 'i', "'", ']']
  &gt;&gt;&gt; literal_eval(str(x))
  ['abc', 'def', 'ghi']
  </code></pre>
  
  <p><strong>Why doesn't <code>list(str(list))</code> turns the <code>str(list)</code> back to the original list?</strong></p>
  
  <p>Or I could use:</p>
  
  <pre><code>&gt;&gt;&gt; eval(str(x))
  ['abc', 'def', 'ghi']
  </code></pre>
  
  <p><strong>Is <code>literal_eval</code> the same as <code>eval</code>? Is <code>eval</code> safe to use?</strong></p>
  
  <p><strong>How many times can I do the following? Does the code break if it keep on doing <code>str(list(str(list))))</code>?</strong> E.g.</p>
  
  <pre><code>&gt;&gt;&gt; x = 'abc'
  &gt;&gt;&gt; list(x)
  ['a', 'b', 'c']
  &gt;&gt;&gt; str(list(x))
  "['a', 'b', 'c']"
  &gt;&gt;&gt; list(str(list(x)))
  ['[', "'", 'a', "'", ',', ' ', "'", 'b', "'", ',', ' ', "'", 'c', "'", ']']
  &gt;&gt;&gt; str(list(str(list(x))))
  '[\'[\', "\'", \'a\', "\'", \',\', \' \', "\'", \'b\', "\'", \',\', \' \', "\'", \'c\', "\'", \']\']'
  &gt;&gt;&gt; list(str(list(str(list(x)))))
  ['[', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'a', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'b', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'c', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ']']
  &gt;&gt;&gt; str(list(str(list(str(list(x))))))
  '[\'[\', "\'", \'[\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'a\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \',\', "\'", \',\', \' \', "\'", \' \', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'b\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \',\', "\'", \',\', \' \', "\'", \' \', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'c\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \']\', "\'", \']\']'
  &gt;&gt;&gt; list(str(list(str(list(str(list(x)))))))
  ['[', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'a', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'b', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'c', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ']']
  </code></pre>