-1

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]) 
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
TheShin
  • 77
  • 5
  • Try to use | operator to merge dictionaries. https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-take-union-of-dictionari – Fran Arenas Jan 31 '22 at 14:30
  • 1
    What you are getting now is indeed an array of dictionaries. what you say you want is an array of just one dictionary. if your array contains a single dictionary why do you want an array in the first place? are you sure this is the structure you want? –  Jan 31 '22 at 14:40
  • 1
    You sure you want a list around your desired result? It only contains one dictionary. Rather keep your dictionary of dictionaries with your indeces as keys. – Anynamer Jan 31 '22 at 14:40

3 Answers3

5

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'}}]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
3

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

Hagai Kalinhoff
  • 606
  • 3
  • 10
-2

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]) 
Simou
  • 682
  • 9
  • 28