Splitting up a list with all values sitting in the same index in Python
<p>Im pretty new to Python.
I have a list which looks like the following:</p>

<pre><code>list = [('foo,bar,bash',)]
</code></pre>

<p>I grabbed it from and sql table (someone created the most rubbish sql table!), and I cant adjust it. This is literally the only format I can pull it in. I need to chop it up. I can't split it by index:</p>

<pre><code>print list[0]
</code></pre>

<p>because that just literally gives me: </p>

<pre><code>[('foo,bar,bash',)]
</code></pre>

<p>How can I split this up? I want to split it up and write it into another list. </p>

<p>Thank you. </p>

<p><code>list = [('foo,bar,bash',)]</code> is a list which contains a tuple with 1 element. You should also use a different variable name instead of <code>list</code> because <code>list</code> is a python built in.</p>

<p>You can split that one element using <code>split</code>:</p>

<pre><code>lst = [('foo,bar,bash',)]
print lst[0][0].split(',')
</code></pre>

<p>Output:</p>

<p><code>['foo', 'bar', 'bash']</code></p>

<hr>

<p>If the tuple contains more than one element, you can loop through it:</p>

<pre><code>lst = [('foo,bar,bash','1,2,3')]
for i in lst[0]:
    print i.split(',')
</code></pre>

python sql list