anvd
anvd

Reputation: 4047

Check if value is present in respective list

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'}

Upvotes: 3

Views: 76

Answers (4)

hygull
hygull

Reputation: 8740

@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'] 

Upvotes: -1

Jacques Gaudin
Jacques Gaudin

Reputation: 16958

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]}

Upvotes: 1

Abhijith Asokan
Abhijith Asokan

Reputation: 1875

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"

Upvotes: 2

jpp
jpp

Reputation: 164623

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]}

Upvotes: 1

Related Questions