I have a function that takes id, name, age, country as an input and puts it in a dictionary within a dictionary like so: {0:{Name: "Name1", Age: 21, Country: "Country1}}
. I plan on putting it inside an array.
I want my array of dictionaries to look like this:
[
{
0: {
'Name': 'Name1',
'Age': 21,
'Country': 'Country1'
},
1: {
'Name': 'Name2',
'Age': 22,
'Country': 'Country2'
},
3: {
'Name': 'Name3',
'Age': 23,
'Country': 'Country3'
}
}
]
However, my current array looks like this:
[
{
0: {
'Name': 'Name1',
'Age': 21,
'Country': 'Country1'
}
},
{
1: {
'Name': 'Name2',
'Age': 22,
'Country': 'Country2'
}
},
{
3: {
'Name': 'Name3',
'Age': 23,
'Country': 'Country3'
}
}
]
There's a subtle difference between the two in terms of how the brackets {} are placed. How do I make it so that my array of dictionaries looks like the first one?
My code:
arr_users = []
def add_entry(id,name,age,country):
users = {}
data = { 'Name': name, 'Age': age, 'Country': country }
users[id] = data
arr_users.append(users)
add_entry(0, "Name1", 21, "Country1")
add_entry(1, "Name2", 22, "Country2")
add_entry(3, "Name3", 23, "Country3")
print(arr_users[0])