Creating an naming variables from an array
<p>I have several arrays:</p>

<pre><code>foo_1 = [URL, 2, 30]
foo_2 = [URL, 4, 1230]
foo_3 = [URL, 11, 980]
foo_4 = [URL, 6, 316]
</code></pre>

<p>... I want to create a function that creates variables and renames them like so:</p>

<pre><code>foo_1Count = foo_1[2]
foo_2Count = foo_2[2]
foo_3Count = foo_3[2]
foo_4Count = foo_4[2]
</code></pre>

<p>I am dealing with a very large set of arrays so creating the variables as such one by one isn't easy.  I don't want to use a dictionary if I can help it.  Is there a way to use .format() to create a variable name or something simple that I am missing?  Thanks!</p>

<p>Dynamically creating variables is usually not a good idea. Instead, you can just get the attribute from the aggregating list object directly. It's even shorter than <code>foo_1Count</code>:</p>

<pre><code>&gt;&gt;&gt; foo_1 = ["URL", 2, 30]
&gt;&gt;&gt; foo_1[2]
30
</code></pre>

<p>But you might not want to memorize which index was the count. Alternatively, create a function:</p>

<pre><code>&gt;&gt;&gt; def count(foo):
...     return foo[2]
... 
&gt;&gt;&gt; count(foo_1)
30
</code></pre>

<p>Or make your <code>foo</code> objects dictionaries:</p>

<pre><code>&gt;&gt;&gt; foo_1 = {"url": "URL", "whatever": 2, "count": 30}
&gt;&gt;&gt; foo_1["count"]
30
</code></pre>

<p>Or use <code>collections.namedtuple</code> to make it a bit cleaner:</p>

<pre><code>&gt;&gt;&gt; Foo = collections.namedtuple("Foo", ["url", "whatever", "count"])
&gt;&gt;&gt; foo_1 = Foo("URL", 2, 30)
&gt;&gt;&gt; foo_1.count
30
</code></pre>

python arrays list