3

I am trying to create a for loop where I check dynamically if some value exists in the respective list. I don't know exactly if I can transform a string in a list, or if there is a better way to do this.

rating_1 = ['no', 'yes'] rating_2 = ['no', 'yes'] for item in d: if d[item] not in item: # I don't want to use the item, # only get name that will match the respective list above print "value not allowed" 

d = {'rating_2': u'no', 'rating_1': u'no'} 
5
  • What does d look like? Commented Jun 4, 2018 at 16:11
  • If you're asking "given a series of lists all having the same name except for different digits at the end, how do I access each one dynamically?", then How do I create a variable number of variables? may be of interest to you. Short version: keep them all in one nested data structure. Commented Jun 4, 2018 at 16:12
  • What does "the respective list" mean? Commented Jun 4, 2018 at 16:13
  • @chrisz question updated. Commented Jun 4, 2018 at 16:14
  • @jdehesa 'rating_1' should match rating_1 list Commented Jun 4, 2018 at 16:16

4 Answers 4

2
my_lists = { 'rating_1' = ['no', 'yes'], 'rating_2' = ['no', 'yes'], } d = {'rating_2': u'no', 'rating_1': u'no'} for item in d: if d[item] not in my_list[item]: print "value not allowed" 

OR, if you want to use variables, use vars() that provides a dictionary of the current namespace, where you can use the variable name as key.

rating_1 = ['no', 'yes'] rating_2 = ['no', 'yes'] d = {'rating_2': u'no', 'rating_1': u'no'} for item in d: if d[item] not in vars()[item]: print "value not allowed" 
Sign up to request clarification or add additional context in comments.

Comments

1

You should use a dictionary for a variable number of variables. Assuming you are looking to perform some sort of validation, you can create a dictionary of invalid items. One way to do this is via iterating the view dict.items:

d = {'rating_2': 'noo', 'rating_1': 'no'} allowed_values = {'rating_2': ['no', 'yes'], 'rating_1': ['no', 'yes']} bad_items = {} for k, v in d.items(): if v not in allowed_values[k]: bad_items[k] = v print(bad_items) {'rating_2': 'noo'} 

Another Pythonic approach is to use a dictionary comprehension:

bad_items = {k: v for k, v in d.items() if v not in allowed_values[k]} 

2 Comments

Obviously, I don't know what values some malicious user will insert to create a dictionary of invalid items.
Sure, you can easily reverse the logic; e.g. reverse the filter by removing the not; and rename as good_items. Really depends on how you want to use this filter.
1

You can use another mapping for the lists of values allowed:

d = {'rating_2': 'no', 'rating_1': 'no'} allowed_values = {'rating_2': ['no', 'yes'], 'rating_1': ['no', 'yes']} is_valid = all(d[item] in allowed_values[item] for item in d) invalid_items = {k: v for k, v in d.items() if v not in allowed_values[k]} 

Comments

-1

@anvd, as I understood from your problem, you want to search the presence of values of dictionary d inside the lists rating1 & rating2.

Please comment, if I'm wrong or the solution that I provided below doesn't satisfy your need.

I will suggest you to create 1 more dictionary d_lists that maps the name of lists to the original list objects.

Steps:

✓ Take each key(list name) from d.

✓ Find the existence of this key in the d_lists.

✓ Take the corresponding list object from d_lists.

✓ Find the existence of value pointed by key in d inside picked list object.

✓ If element found then stop iteration and search the presence of next value in d.

✓ Print relevant messages.

Here is your modified code (with little modification)

I have shown another good example later after the below code example.

rating_1 = ['nlo', 'yes'] rating_2 = ['no', 'yes'] # Creating a dictionary that maps list names to themselves (original list object) d_lists = { "rating_1": rating_1, "rating_2": rating_2, } # Creating a dictionary that maps list to the item to be searched # We just want to check whether 'rating_1' & 'rating_2' contains 'no' or not d = {'rating_2': u'no', 'rating_1': u'no'} # Search operation using loop for list_name in d: if list_name in d_lists: found = False for item in d_lists[list_name]: if item == d[list_name]: found = True break if found: print "'" + d[list_name] + "' exists in", d_lists[list_name]; else: print "'" + d[list_name] + "' doesn't exist in", d_lists[list_name] else: print "Couldn't find", list_name 

Output:

'no' exists in ['no', 'yes'] 'no' doesn't exist in ['nlo', 'yes'] 

Now, have a look at another example.

Another big example:

rating_1 = ['no', 'yes', 'good', 'best'] rating_2 = ['no', 'yes', 'better', 'worst', 'bad'] fruits = ["apple", "mango", "pineapple"] # Creating a dictionary that maps list names to themselves (original list object) d_lists = { "rating_1": rating_1, "rating_2": rating_2, "fruits": fruits, } # Creating a dictionary that maps list to the item to be searched # We just want to check whether 'rating_1' & 'rating_2' contains 'no' or not d = {'rating_2': u'best', 'rating_1': u'good', 'fruits2': "blackberry"} # Search operation using loop for list_name in d: if list_name in d_lists: print "Found list referred by key/name", list_name, "=", d_lists[list_name]; found = False for item in d_lists[list_name]: if d[list_name] == item: found = True break if found: print "'" + d[list_name] + "' exists in", d_lists[list_name], "\n"; else: print "'" + d[list_name] + "' doesn't exist in", d_lists[list_name], "\n" else: print "Couldn't find list referred by key/name ", list_name, "\n" 

Output:

Found list referred by key/name rating_2 = ['no', 'yes', 'better', 'worst', 'bad'] 'best' doesn't exist in ['no', 'yes', 'better', 'worst', 'bad'] Couldn't find list referred by key/name fruits2 Found list referred by key/name rating_1 = ['no', 'yes', 'good', 'best'] 'good' exists in ['no', 'yes', 'good', 'best'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.