python iterate over a list and remove any duplicates found within lists inside a dictionary and remove the duplicates -


i have list of numbers , dictionary names(key) , numbers(values) need iterate on list called lotto , and check them against values of names in dictionary , if match remove number dictionary value.

heres have far prints out origanal dictionary keys , values

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]} lotto =[1,2,3,4,5,6,]  in players.values():       if in lotto:        players.values.remove(i)    print (players) 

any appriciated

you accessing dict values in wrong way. take @ following:

players = {'ray': [1,2,3,6,7,8,], 'al':[1,2,3,4,8,9,]} lotto =[1,2,3,4,5,6,]  p, val in players.items():       num in lotto:         if num in val:             players[p].remove(num)  print (players)  # {'al': [8, 9], 'ray': [7, 8]} 

if order of items in list-values not important you, can use following, faster, variant:

players = {'ray': [1,2,3,6,7,8], 'al':[1,2,3,4,8,9]} lotto = [1,2,3,4,5,6] lotto = set(lotto)  p, val in players.items():     players[p] = list(set(val) - lotto)  print (players)  # {'ray': [8, 7], 'al': [8, 9]} 

which can condensed in following one-liner (it recreates dict though instead of modifying it):

players = {k: list(set(val) - lotto) k, v in players.items()} 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -