I have 4 list and each element on the list are unique across the 4 list. How do I find if the value exist in one of the list and return which list it exist?
Example
If you don't want to use a list comprehension, you can always use a convoluted and verbose lambda expression!
list(filter(lambda x: value in x, [a,b,c,d]))
EDIT: Question was updated to ask for var name, not var value
What you are trying to do is almost always a bad idea. Suppose you do get a string containing the name of the list you want. What do you do with it then? There's no nice way to get a variable if all you have is a string containing its name. You can do it with globals()
or locals()
, but those inevitably make your code messy and confusing.
The correct way to do this is to use a list or a dictionary to collect all of the lists you want to search through. Iterating through a single object is much better practice than iterating through every name in the global or local scope.
value = "a"
named_lists = {
"a": ['a','b','c'],
"b": ['d','e','f'],
"d": ['g','h','i'],
"c": ['j','k','l']
}
names = [name for name, seq in named_lists.items() if value in seq]
print(names)
if names: #there may be multiple lists that contain `value`, so let's just look at the first one, if it exists
first_name = names[0]
print(named_lists[first_name])
Result:
['a']
['a', 'b', 'c']
Given your updated question, let's assume the a, b, c, d
variables are in the global scope
value = 'a'
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list), globals().items()) if value in v)
print(w)
produces
a
i.e. the name of the first list in the global namespace that contains value
If the variables are in a local scope, e.g. within a function, you can use locals()
instead
def f():
a = ['a','b','c']
b = ['d','e','f']
d = ['g','h','i']
c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list), locals().items()) if value in v)
print(w)
f()
produces
a
Note: you might want to adopt a naming convention to target a specific group of variables, e.g. targ_
as a prefix
targ_a = ['a','b','c']
targ_b = ['d','e','f']
targ_d = ['g','h','i']
targ_c = ['j','k','l']
w = next(n for n,v in filter(lambda t: isinstance(t[1],list) and t[0].startswith('targ_'), globals().items()) if value in v)
print(w)
produces
targ_a
To explain things a bit more in detail, let's have a look at what the globals()
call returns. For example using the python shell
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'a'
>>> targ_a = ['a','b','c']
>>> targ_b = ['d','e','f']
>>> targ_d = ['g','h','i']
>>> targ_c = ['j','k','l']
>>> globals()
{'targ_d': ['g', 'h', 'i'], 'targ_c': ['j', 'k', 'l'],
'__builtins__': <module 'builtins' (built-in)>,
'__doc__': None, '__package__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'targ_a': ['a', 'b', 'c'], '__name__': '__main__',
'targ_b': ['d', 'e', 'f'], '__spec__': None, 'value': 'a'}
as you can see, globals()
returns a dictionary. Its keys are the names of the variables defined in the global namespace. Its values are the value held by each such variable.
Therefore
>>> next(n for n,v in filter(lambda t: isinstance(t[1],list) and t[0].startswith('targ_'), globals().items()) if value in v)
'targ_a'
iterates once on the generator produced by the expression, which yields each name in the global namespace that corresponds to a list whose name starts with targ_
and that contains an element equal to value
. It does so by iterating on the dictionary returned by the call to globals
you can use list comprehension
:
>>> value ="a"
>>> a = ['a','b','c']
>>> b = ['d','e','f']
>>> d = ['g','h','i']
>>> c = ['j','k','l']
>>> [x for x in a,b,c,d if value in x]
[['a', 'b', 'c']]
To get variable name:
>>> for x in a,b,c,d:
... if value in x:
... for key,val in locals().items():
... if x == val:
... print key
... break
...
a
locals() contains local scope variable as dictionary, variable name being key and its value being value
globals contains global scope variable as dictionary, variable name being key and its value being value