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>If you're asking how to rename (understanding this action like create a new variable and deleting the existing old one) you could manipulate <a href="https://docs.python.org/2/library/functions.html#globals" rel="nofollow">globals</a> like this:</p>

<pre><code>if __name__ == "__main__":
    URL = 'www.abc.com'

    foo_1 = [URL, 2, 30]
    foo_2 = [URL, 4, 1230]
    foo_3 = [URL, 11, 980]
    foo_4 = [URL, 6, 316]

    for i in range(4):
        old_name = 'foo_{}'.format(i + 1)
        new_name = 'foo_{}Count'.format(i + 1)
        globals()[new_name] = globals()[old_name][2]
        del globals()[old_name]

    print globals()
</code></pre>

<p>Now, I strongly recommend you not doing this! Don't mess with globals manually... Instead, if your variables have a similar structure, <strong>usually</strong> there isn't any good reason to declare them individually, you could pack them like this:</p>

<pre><code>URL = 'www.abc.com'

foos = [
    [URL, 2, 30],
    [URL, 4, 1230],
    [URL, 11, 980],
    [URL, 6, 316]
]

foo_counts = [foo[2] for foo in foos]
print foo_counts
</code></pre>

<p>That way, you can iterate over your data nicely without having any cheap trick like messing around with globals.</p>

python arrays list