Why am I receiving an error when trying to compare two lists?
<p>I am trying to compare two lists on line <code>#12</code> and return matches found. </p>

<p>The lists contain one user selected number (<code>un</code>) and one of which have been randomly generated (<code>rn</code>).</p>

<p>For example, <code>[['1', '5', '3', '7']]</code> and <code>[['9', '6', '3', '2']]</code> would return <code>[3]</code>.</p>

<p>I am fairly new to python and was using the solution found <a href="http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches">HERE</a>, but am yet to have success with my code.</p>

<pre><code>import random
import re

rn = []
un = []

Numbers = range(1000,9999)
RandomNumber = random.choice(Numbers)
RandomNumber = str(RandomNumber)

def check():
    x = set(rn) &amp; set(un) #12
    print (x)

def numsys():
    b = list(RandomNumber)
    rn.append(b)
    print(rn)
    print(un)
    check()


def numval():
    while True:
        UserNum = (input("Please enter a 4 digit number: "))
        if re.match("^[0-9]{4,4}$", UserNum):
            a = list(UserNum)
            un.append(a)
            numsys()
            break        
numval()
</code></pre>

<p>When you cast a list to a string back and forth, the result wouldn't be the original string. It would be a list containing the characters of the representation of the original string. </p>

<pre><code>&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; b = str(a)
&gt;&gt;&gt; c = list(b)
&gt;&gt;&gt; c
['[', '1', ',', ' ', '2', ',', ' ', '3', ']']
</code></pre>

<p>Don't do such castings, and if you have to, use <code>','.join(map(str, a))</code> to cast to string, and <code>list(map(int, b.split(',')))</code> to cast to list back.</p>

python list python-3.x string-comparison list-comparison