2

I have such a code statement:

def add_names(req, names, firstnames, middlenames, lastnames, genders, e_mails, phone_numbers, locations, indata):
        data = []
        for a in range(len(names)):

                nxt = {"name": names[a], "profile_id": indata["profile_id"], "firstname": firstnames[a], "middlename": middlenames[a], "lastname": lastnames[a],
                       "e_mail": e_mails[a], "phone_number": phone_numbers[a], "location": locations[a], "sex": genders[a],
                       "user_id": indata["user_id"], "updated_by": indata["username"]}

                cpnxt = nxt
                del cpnxt['profile_id'], cpnxt['user_id'], cpnxt['updated_by']
                nxt.update({"jsondata": cpnxt})
                data.append(nxt)

        qs = "insert into persons (id, name, user_id, profile_id, firstname, middlename, lastname, e_mail, phone_number, location, sex, updated_by) values "
        qs += "(DEFAULT, %(name)s, %(user_id)s, %(profile_id)s, %(firstname)s, %(middlename)s, %(lastname)s, %(e_mail)s, %(phone_number)s, %(location)s, %(sex)s,"
        qs += " %(updated_by)s) returning id"
        res = db_add_list(req, qs, data)
        return res

Here, basically I get list values as parameters, and put the list values into the json dictionary called nxt{}, and add them into the database by using the query "qs" through db_add_list() function.

Now, what I want to do is; I want to add a json dictionary into the nxt{} dictionary. My dictionary will be the subset of nxt{} dictionary, which involves only some values of nxt{}.

I want to do that with this code, but I get such a weird output:

nxt = {'e_mail': '', 'firstname': 'John', 'jsondata': {'e_mail': '', 'firstname': 'John', 'jsondata': {'e_mail': '', 'firstname': 'John', 'jsondata': {'e_mail': '', 'firstname': 'John', 'jsondata': {'e_mail': '', 'firstname': 'John', 'jsondata': {'e_mail': '', 'firstname': 'John', 'jsondata': {...}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}, 'lastname': 'Smith', 'location': '', 'middlename': '', 'name': 'John Smith', 'phone_number': '', 'sex': 'M'}

How can I manage to do it?

3 Answers 3

1

Just update your nxt dictionary and add your new subset dictionary to it.

nxt.update({"new_dic": my_new_dic})
Sign up to request clarification or add additional context in comments.

3 Comments

it is weird but there is a strange loop in updating the dict.
can u update your code? so i can see what u did there.
@yusuf Sorry for interrupt, the problem is with your key jsondata, you're updaing same key values every time, you should change jsondata name in for loop iteration like jsondata1, jsondata2 and so on.
1

You can add it as a key value pair for the nxt dictionary. like this:

nxt['subset'] = {'key': 'value'}

Comments

1

Both solutions described earlier work, i used pythons shell to show you.

In [1]: next = {}

In [2]: indexer = {}

In [3]: next['indexer'] = indexer

In [4]: upd = {}

In [5]: next.update({'upd' : upd})

In [6]: next

Out[6]: {'indexer': {}, 'upd': {}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.