Comparing items of case-insensitive list
<p>So I have two lists, with items in varying cases. Some of the items are the same but are lowercase/uppercase. How can I create a loop to run through all the items and do something with each item, that also ignores what case the item may have?</p>

<pre><code>fruits = ['Apple','banana','Kiwi','melon']
fruits_add = ['apple','Banana','KIWI','strawberry']
</code></pre>

<p>I want to create a loop that goes through each item in <code>fruits_add</code> and adds it to <code>fruits</code>, if the item is not already in <code>fruits</code>. However an item like <code>'apple'</code> and <code>'Apple'</code> need to count as the same item.</p>

<p>I understand how to convert individual items to different cases and how to check if one specific item is identical to another (disregarding case). I don't know how to create a loop that just does this for all items.</p>

<p>Found answers to similar questions for other languages, but not for Python 3.</p>

<p>My attempt:</p>

<pre><code>for fruit.lower() in fruits_add:
    if fruit in fruits:
        print("already in list")
</code></pre>

<p>This gives me an error:     </p>

<pre><code>SyntaxError: can't assign to function call
</code></pre>

<p>I've also tried converting every item in each list to lowercase before comparing the lists, but that doesn't work either.</p>

<p><code>fruit.lower()</code> in the for loop won't work as the error message implies, you can't assign to a function call..</p>

<p>What you could do is create an auxiliary structure (<code>set</code> here) that holds the lowercase items of the existing fruits in <code>fruits</code>, and, <code>append</code> to <code>fruits</code> if a <code>fruit.lower()</code> in <code>fruit_add</code> isn't in the <code>t</code> set (containing the lowercase fruits from <code>fruits</code>):</p>

<pre><code>t = {i.lower() for i in fruits}
for fruit in fruits_add:
    if fruit.lower() not in t:
        fruits.append(fruit)
</code></pre>

<p>With <code>fruits</code> now being:</p>

<pre><code>print(fruits)
['Apple', 'banana', 'Kiwi', 'melon', 'strawberry']
</code></pre>

python list python-3.x compare case-insensitive