Home > Mobile >  How do I get this kind of array of dictionaries
How do I get this kind of array of dictionaries

Time:02-01

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]) 

CodePudding user response:

Your code is producing a list of dictionaries. What you seem to want is a list containing just one dictionary. Therefore:

def add_entry(id_, name, age, country, d):
    d[id_] = { 'Name': name, 'Age': age, 'Country': country }   

d = dict()

add_entry(0, "Name1", 21, "Country1", d)
add_entry(1, "Name2", 22, "Country2", d)
add_entry(3, "Name3", 23, "Country3", d)

arr_users = [d]
print(arr_users)

Output:

[{0: {'Name': 'Name1', 'Age': 21, 'Country': 'Country1'}, 1: {'Name': 'Name2', 'Age': 22, 'Country': 'Country2'}, 3: {'Name': 'Name3', 'Age': 23, 'Country': 'Country3'}}]

CodePudding user response:

each time you invoke the function you append a new dict to the array, while you want the array to have a single dict containing all the data, so you need to modify the function:

arr_users = [{}]

def add_entry(id,name,age,country):
    obj = arr_users[0]

    data = { 'Name': name, 'Age': age, 'Country': country }
    obj[id] = data

also note that you overshadow the built-in id function which is somehow a bad practice maybe change the name to _id or entity_id

CodePudding user response:

I don't usually share solution as code directly, but here is how it should be.

arr_users = []

def add_entry(id,name,age,country):

    users = {}

    data = {'Name': name, 'Age': age, 'Country': country}

    users[id] = data
    if len(arr_users) == 0:
        arr_users.append(users)
    else:
        arr_users[0] = {**arr_users[0], **users}


add_entry(0, "Name1", 21, "Country1")
add_entry(1, "Name2", 22, "Country2")
add_entry(3, "Name3", 23, "Country3")

print(arr_users[0]) 
  •  Tags:  
  • Related