Avoid extra line for attribute check?
<p>I am developing this Python project where I encounter a situation many times and I wondered if there is a better way.</p>

<p>There is a list of class instances. Some part of lists are empty(filled with <code>None</code>).
Here is an example list.</p>

<pre><code>ins_list = [ins_1, ins_2, None, ins_3, None]
</code></pre>

<p>I have to do some confirmations throughout the program flow. There are points where I need the control an attribute of these instances. But only indexes are given for choosing an instance from the list and it may be one of the empty elements. Which would give an error when the attribute is called. Here is an example program flow.</p>

<pre><code>ind = 2

if ins_list[ind].some_attribute == "thing":
    # This would give error when empty element is selected.
</code></pre>

<p>I deal with this by using,</p>

<pre><code>if ins_list[ind]:
    if ins_list[ind].some_attribute == "thing":
        # This works
</code></pre>

<p>I am okay with using this. However the program is a long one, I apply this hundreds of times. Is there an easier, better way of doing this, it means I am producing reduntant code and increasing indentation level for no reason. I wish to know if there is such a solution.</p>

<p>There are these two options:</p>

<blockquote>
  <p>Using a dictionary:</p>
</blockquote>

<p>Another way would be to use a <code>dictionary</code> instead. So you could create your dictionary once the list is filled up with elements. The dictionary's keys would be the values of your list and as values you could use the attributes of the elements that are not None and "No_attr" for those that are None. (<strong>Note:</strong> Have in mind that python dictionaries don't support duplicate keys and that's why I propose below to store as keys your list indexes else you will have to find a way to make keys be different)</p>

<p>For example for a list like:</p>

<pre><code>l = [item1,item2,None,item4]
</code></pre>

<p>You could create a dictionary:</p>

<pre><code>d = {item1:"thing1", item2:"thing2", None:"No_attr", item3:"thing3"}
</code></pre>

<p>So in this way every time you would need to make a check, you wouldn't have to check two conditions, but you could check only the value, such as:</p>

<pre><code>if d.values()[your_index]=="thing":
</code></pre>

<p>The only <strong>cons</strong> of this method is that standard python dictionaries are <strong>inherently unordered</strong>, which makes accessing dictionary values by index a bit dangerous sometimes - you have to be careful not to change the form-arrangement of the dictionary. </p>

<p>Now, if you want to make sure that the <strong>index stays stable</strong>, then you would have to store it some way, for example select as <strong>keys</strong> of your dictionary the indexes, as you will have already stored the attributes of the items - <strong>But</strong> that is something that you will have to decide and depends strongly on the architecture of your project.</p>

<blockquote>
  <p>Using a list:</p>
</blockquote>

<p>In using <code>list</code>s way I don't think there is a way to avoid your if statement - and is not bad actually. Maybe use an <code>and</code> operator as it is mentioned already in another answer but I don't think that makes any difference anyway.</p>

<p><strong>Also</strong>, if you want to use your first approach:
<code>if ins_list[ind].some_attribute == "thing":</code></p>

<p>You could try using and exception catcher like this:</p>

<pre><code>try:
    if ins_list[ind].some_attribute == "thing":
        #do something
except:  
    #an error occured
    pass
</code></pre>

python attributes