You can also create a new dict from your current dicts in which each dict["name"] value will be associated with a dict["color"] value.
For example: the new dict will look like this:
{"Jhonny": "green", "Steve": "blue"}
And you can use a function similar to the example below, which takes a lot of arguments and returns the desired list (In addition, it adds None if the input list has any name that is not in the default files):
Here is my example:
a = { "name" : "Johnny", "color" : "green" } b = { "name" : "Steve", "color" : "blue" } c = { "name" : "Ben", "color" : "red" } d = { "name" : "Timmy", "color" : "cyan" } my_list = ["Steve", "Steve", "Ben", "Ben", "Johnny"] def iter_func(my_list = list, *args): ne = {k["name"]:k["color"] for k in args} return [ne[k] if k in ne.keys() else None for k in my_list]
Output:
print(iter_func(my_list, a,b,c,d)) >>> ['blue', 'blue', 'red', 'red', 'green']
Example with None values:
a = { "name" : "Johnny", "color" : "green" } b = { "name" : "Steve", "color" : "blue" } c = { "name" : "Ben", "color" : "red" } d = { "name" : "Timmy", "color" : "cyan" } my_list = ["Steve", "Steve", "Alex", "Ben", "Ben", "Johnny", "Mark"] def iter_func(my_list = list, *args): ne = {k["name"]:k["color"] for k in args} return [ne[k] if k in ne.keys() else None for k in my_list]
Output:
print(iter_func(my_list, a,b,c,d)) >>> ['blue', 'blue', None, 'red', 'red', 'green', None]