This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How can I find the elements from a list in the keys from a dictionary?

I have created a dictionary and I am trying to find if the elements in a list called "seqrecord" are the keys of the dictionary. The script I have done is below:

def read_rebase(Renzymes):
    renzymedict = {}
    
    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()

    return renzymedict
    ren = {}
    ren = read_rebase(Renzymes)

for k in seqrecord:
    if k in ren:

        print k, ren[k]

It doesn't work...
NameError: name 'ren' is not defined

What I am doing wrong? 'ren' should be the dictionary...

Thank you

python list dictionary biopython

after a return statement nothing will be executed! the function returns to its call by returning renzymedict

return renzymedict

ren = {} #not reached!
ren = read_rebase(Renzymes)

I have deleted return statement, but the result is the same ('ren' is not defined)

Maybe your indentation is wrong in your posted code?

try this

def read_rebase(Renzymes):
    renzymedict = {}

    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()

    return renzymedict

ren = {}

ren = read_rebase(Renzymes)

for k in seqrecord:
    if k in ren:
        print k, ren[k]

I don't think so,

ren = {}
ren = read_rebase(Renzymes)

are "inside" the def read_base function...

but this will not work. if you put it outside of the function it will work. it works like this:

ren = {} #initialize a empty dict

ren = read_rebase(Renzymes) # call the function read_rebase(Renzymes) which returns renzymdict, which is a dict

now ren is declared and a known variable which can be used in

if k in ren:

Ok, modified this error, now there is a new one...

NameError: name 'Renzymes' is not defined

(Thank you for your answers)

Renzymes is variable which should contain your filename.

def read_rebase(Renzymes):
    renzymedict = {}

    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()
    return renzymedict

ren = {}

ren = read_rebase("/your/path/and/file.txt")

for k in seqrecord:
    if k in ren:
        print k, ren[k]

Here is how you defined your function read_rebase:

def read_rebase(Renzymes):
    ...

When you call this function, you need to provide some value for the local variable Renzymes that is a path to a file somewhere. For example:

ren = read_rebase("/your/path/and/file.txt")

The file /your/path/and/file.txt will get opened in the function read_rebase.

If you instead call this function like so:

ren = read_rebase(Renzymes)

Then you need to have defined a global variable (also poorly and ambiguously named as Renzymes) somewhere further up in the code. If this variable is not defined, then the read_rebase function cannot work.

So either set the path to the file explicitly, as shown above, or set a sensibly-named variable and pass that to your function, e.g.:

path_to_Renzymes_file = "/foo/bar/baz.txt"
ren = read_rebase(path_to_Renzymes_file)

2 answers

Use the built-in set functions of Python, eg:

s = set(seqrecord= ['a', 'b', 'd', 'e'])
m = set(myDict= {'a': 0, 'd': 0, 'f': 0})

s.intersection(m)

s.difference(m)

m.difference(s)

You do not have to transform them into sets before hand, of course. It can be done on the fly, just substitute s or m by set(...)

To intersect lists, i.e. to find what elements in a list are or are not present in another list, it is good to use list comprehensions, they are fast and readable:

seqrecord= ['a', 'b', 'd', 'e']
myDict= {'a': 0, 'd': 0, 'f': 0}

## Elements in common between seqrecord and keys
[x for x in seqrecord if x in myDict.keys()]
['a', 'd']

## Elements in seqrecord NOT in keys
[x for x in seqrecord if x not in myDict.keys()]
['b', 'e']

## Keys NOT in seqrecord
[x for x in myDict.keys() if x not in seqrecord]
['f']

Log in to answer this question.