How to update a composite list as contents of member lists are changed?
<p>Let's say I have:</p>

<pre><code>friendlies = []
enemies = []
everyone = [friendlies + enemies]
</code></pre>

<p>And I say:</p>

<pre><code>friendlies.append("something")
</code></pre>

<p>so that friendlies now contains:</p>

<pre><code>["something"]
</code></pre>

<p>What is the pythonic way to make it so that <code>everyone</code> updates as well? So that if I update <code>friendlies</code> in the way stated above, <code>everyone</code> would contain:</p>

<pre><code>["something"] 
</code></pre>

<p>?</p>

<p>Maintain a reference to <code>friendlies</code> and <code>enemies</code> in the composite list:</p>

<pre><code>everyone = [friendlies, enemies]
#                     ^
</code></pre>

<p>Index 0 and 1 of the compisite list will reference <code>friendlies</code> and <code>enemies</code> respectively. </p>

<p><code>friendlies + enemies</code> creates a new list which is not what you want.</p>

<hr>

<p>A more readable approach will be making <code>everyone</code> a dictionary:</p>

<pre><code>everyone = {'friendlies': friendlies, 'enemies': enemies}
</code></pre>

<p>And can be used as:</p>

<pre><code>&gt;&gt;&gt; friendlies = []
&gt;&gt;&gt; enemies = []
&gt;&gt;&gt; everyone = {'friendlies': friendlies, 'enemies': enemies}
&gt;&gt;&gt; friendlies.append("something")
&gt;&gt;&gt; everyone['friendlies']
['something']
</code></pre>

python list python-3.x data-structures