2

I have a large database from the following type:

data = {
    "2": {"overall": 172, "buy": 172, "name": "ben", "id": 2, "sell": 172},
    "3": {"overall": 173, "buy": 173, "name": "dan", "id": 3, "sell": 173},
    "4": {"overall": 174, "buy": 174, "name": "josh", "id": 4, "sell": 174},
    ...

and so on for about 10k rows.

Then, I created a loop to find if inside this dict() there are specific names: I used the next loop

items = ["ben","josh"]

Database = dict()
Database = {"Buying_Price": "", "Selling_Price": ""}
for masterkey, mastervalue in data.items():
    if mastervalue['name'] in items:
        Database["Name"] = Database["Name"].append(mastervalue['name'])
        Database["Buying_Price"] = Database["Buying_Price"].append(mastervalue['buy'])
        Database["Selling_Price"] = Database["Selling_Price"].append(mastervalue['sell'])

However, I'm getting the next error:

Database["Buying_Price"] = Database["Buying_Price"].append(mastervalue['buy_average'])
AttributeError: 'str' object has no attribute 'append'

My goal is to obtain a dict names Database with 2 keys: Buying_Price,Selling_Price where in each one I will have the following:

Buying_Price = {"ben":172,"josh":174}
Sellng_Price = {"ben":172,"josh":174}

Thank you.

8
  • I'm confused. Is your program running without crashing and producing the wrong results ("only the value of the last item as for 'josh' ")? Or is it crashing and producing no results ("Im getting the next error")? Or... Both, somehow? Do you have a try-except you're not showing us? Commented Feb 2, 2018 at 14:55
  • 1
    It looks like you are... trying to "append" a string to another string! [exactly as the error says], can you specify what you want as the output? Commented Feb 2, 2018 at 14:57
  • 1
    You have a lot of problems with the code, but the one you are referring to in the post is that you have defined Database["Buying_Price"] as a string and string object does not have the .append() method, only lists do. Commented Feb 2, 2018 at 14:57
  • Is there any suggestion then how to obtain the result im looking for? Commented Feb 2, 2018 at 14:58
  • 1
    @Ben what result are you looking for? Commented Feb 2, 2018 at 14:58

3 Answers 3

2

There are a couple of issues with the code you posted, so we'll go line by line and fix them:

items = ["ben", "josh"]

Database = dict()
Database = {"Buying_Price": "", "Selling_Price": ""}
for masterkey, mastervalue in data.items():
    if mastervalue['name'] in items:
        Database["Name"] = Database["Name"].append(mastervalue['name'])
        Database["Buying_Price"] = Database["Buying_Price"].append(mastervalue['buy_average'])
        Database["Selling_Price"] = Database["Selling_Price"].append(mastervalue['sell_average'])
  1. In Python, you don't need to define the object type explicitly and then assign its value, so it means that Database = dict() is redundant since you already define this to be a dictionary the line below.
  2. You intend to aggregate your results of the if statement so both Buying_Price and Selling_Price should be defined as lists and not as strings. You can either do it by assigning a [] value or the literal list().
  3. According to your data structure, you don't have the buy_average and sell_average keys, only buy and sell so make sure you use the correct keys.
  4. You don't need to re-assign your list value when using the append() method, it's the object's method so it will update the object in-place.
  5. You didn't set what Name is in your Database object and yet you're trying to append values to it.

Overall, the code should roughly look like this:

items = ["ben","josh"]

Database = {"Buying_Price": [], "Selling_Price": [], "Name": []}
for masterkey, mastervalue in data.items():
    if mastervalue['name'] in items:
        Database["Name"].append(mastervalue['name'])
        Database["Buying_Price"].append(mastervalue['buy'])
        Database["Selling_Price"].append(mastervalue['sell'])
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like you want a nested dict.

items = ["ben", "josh"]

new_database = {"Buying_Price": {}, "Selling_Price": {}}

for key, row in data.items():
    name = row["name"]

    if name in items:
        new_database["Buying_Price"][name] = row["buy"]
        new_database["Selling_Price"][name] = row["sell"]

Comments

0

In Database = {"Buying_Price": "", "Selling_Price": ""}, you are defining the key Buying_Price as "" : meaning a string. You are trying to use the .append() list method into a string, hence the error 'str' object has no attribute 'append'.

We do not know the output you want, but seeing how you want to compute your data, I suggest you do :

Database = {"Name" : [], "Buying_Price": [], "Selling_Price": []}

instead of the original...

Database = {"Buying_Price": "", "Selling_Price": ""}

This way, you will be able to append your data Name, Buying_Price, and Selling_Price at the same time, and you'll be able to make search and access data of all the arrays using the index of only one.


I haven't paid attention, but you are badly appending your data to your dict.

.append() will work in-place, meaning that you should do :

Database["Name"].append(mastervalue['name'])

instead of

Database["Name"] = Database["Name"].append(mastervalue['name'])

2 Comments

then i get: Database["Buying_Price"] = Database["Buying_Price"].append(mastervalue['buy']) AttributeError: 'NoneType' object has no attribute 'append'
Yes, because .append() returns None. Remove the assignment from those statement

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.