I am trying to create a dictionary for Gray using the all_Artists list. His list need to include everything that is not in Isaac's list.
Isaac = {'fav_Artists': ['Little River Band', 'Juice Wrld', 'HP Boyz', 'Ne-Yo']}
Grays = {'fav_Artists': []}
all_Artists = ['Weird Al', 'Bruno Mars', 'Juice Wrld', 'Ricky Martin', 'HP Boyz', 'James Blunt', 'Ne-Yo', 'Paul McCartney', 'Little River Band']
for i in all_Artists:
if not i in Isaac['fav_Artists']:
Grays['fav_Artists'] = [i]
print(Isaac)
print(Grays)
The current output is:
{'fav_Artists': ['Little River Band', 'Juice Wrld', 'HP Boyz', 'Ne-Yo']}
{'fav_Artists': ['Paul McCartney']}
Can anyone tell me why I am only getting Paul and not the others?
You are setting Grays['fav_Artists']
to the new value each time. Basically, you are overwriting previous values with a new value, which is not what you want. Instead, use the append
function:
Isaac = {'fav_Artists': ['Little River Band', 'Juice Wrld', 'HP Boyz', 'Ne-Yo']}
Grays = {'fav_Artists': []}
all_Artists = ['Weird Al', 'Bruno Mars', 'Juice Wrld', 'Ricky Martin', 'HP Boyz', 'James Blunt', 'Ne-Yo', 'Paul McCartney', 'Little River Band']
for i in all_Artists:
if not i in Isaac['fav_Artists']:
Grays['fav_Artists'].append(i)
print(Isaac)
print(Grays)
Give this a try.
Isaac = {'fav_Artists': ['Little River Band', 'Juice Wrld', 'HP Boyz', 'Ne-Yo']}
Grays = {'fav_Artists': []}
all_Artists = ['Weird Al', 'Bruno Mars', 'Juice Wrld', 'Ricky Martin', 'HP Boyz', 'James Blunt', 'Ne-Yo', 'Paul McCartney', 'Little River Band']
for artists in all_Artists:
if artists not in Isaac['fav_Artists']:
Grays['fav_Artists'].append(artists)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.