hey i was trying to to change the elements in the dictionary using the if statement in a for loop , when i include the properties dictionary in the first for loop , it seems to be working fine. otherwise i cant change the elements as i desired .
what i was trying to do is , creating an empty list . then adding 30 dictionary items with same attributes. after the dictionary is created , i was trying to change the attributes of first 3 elements in the list using a if statement . then printing the first 6 elements in the list to check whether the change is applied or not
properties1={'color':'silver','weight':45,'Height':5.5,'planet':'mars'}
for alien in range(30):
aliens.append(properties1)
for alien in aliens[0:3]:
if alien['color'] == 'silver':
alien['weight']=10
alien['Height']=2
print(alien)
for alien in aliens[:6]:
print(alien)
output is
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}
{'color': 'silver', 'weight': 10, 'Height': 2, 'planet': 'mars'}