How to use str as list index in Python?
<p>I read the name and the points of each person from a database and I need to have something like that:</p>

<pre><code>myarray['Alex'] = 18
</code></pre>

<p>I've tried this :</p>

<pre><code>myArray = []
cur.execute("SELECT name, point FROM mytable WHERE name &lt;&gt; '' ")

for row in cur.fetchall():
    name = row[0]
    myArray[name] = row[1]
</code></pre>

<p>but I got this error</p>

<pre><code>TypeError: list indices must be integers, not str
</code></pre>

<p>You need to use a <a href="https://docs.python.org/2/tutorial/datastructures.html#dictionaries" rel="nofollow">dictionary</a>, not an array:</p>

<pre><code>myDict = {} # Here!
cur.execute("SELECT name, point FROM mytable WHERE name &lt;&gt; '' ")

for row in cur.fetchall():
    name = row[0]
    myDict[name] = row[1]
</code></pre>

python