I have the following code which runs through a list of words and increments variables representing colors as follows:
white = 0
gold = 0
silver = 0
grey = 0
lavender = 0
pink = 0
red = 0
green = 0
blue = 0
#list of colors
colors = ['white','gold','silver','grey','lavender','pink','red','green','blue']
#increment color variables accordingly
for i in wordList:
if colors[0] in i:
white+=1
if colors[1] in i:
gold+=1
if colors[2] in i:
silver+=1
if colors[3] in i:
grey+=1
if colors[4] in i:
lavender+=1
if colors[5] in i:
pink+=1
if i == colors[6]:
red+=1
if colors[7] in i:
green+=1
if colors[8] in i:
blue+=1
I feel like there must be a better way to do this, but I'm not sure what it is. Thanks.
dict:{'white': 0, 'gold': 0, ...}. Then use a second for loop to check each key in the list and increment the value by 1.