Home > Back-end >  create a loop that will output the age and name of each. in the format "name(has age years)&quo
create a loop that will output the age and name of each. in the format "name(has age years)&quo

Time:09-21

the question is: to create a loop that will output the name and age of each.

example =[
    
    {"name":"Ana","age": 16,"country":'india','hobbies':['futboll']} , 
    {'name' :'Leo ' ,'age' : 20 , ' country ' : ' USA' ,'hobbies ' : ['fishing']},
    {'name':'Mia ' , 'age' : 64 , 'country ' : 'cuba' , ' hobbies ' :['read']},
    {'name':'Ian','age':43,'country':'france','hobbies':[tennis]} 
    ]

CodePudding user response:

for i in example:
    print(i['name'], i['age'])

CodePudding user response:

Try this:

example =[
    
    {"name":"Ana","age": 16,"country":'india','hobbies':['futboll']} , 
    {'name' :'Leo' ,'age' : 20 , ' country ' : ' USA' ,'hobbies ' : ['fishing']},
    {'name':'Mia' , 'age' : 64 , 'country ' : 'cuba' , ' hobbies ' :['read']},
    {'name':'Ian','age':43,'country':'france','hobbies':['tennis']} 
]

for ex in example:
    print('{} (has {} yesrs)'.format(ex['name'], ex['age']))          

Output:

Ana (has 16 yesrs)
Leo (has 20 yesrs)
Mia (has 64 yesrs)
Ian (has 43 yesrs)

CodePudding user response:

for i in example:
    print(i["name"], i["age"])

You can use python dictionaries with key-value pairs. It is fundamental to the python programming language. Personal suggestion, more tutorials on it.

  • Related