0

I was wondering if there is a way to access specific dictionary values within user-defined objects?

E.g.

class Test():
    def __init__(self, a, b, c):
        self.a=a
        self.b=b
        self.c=c

test1=Test("text", 0, {"key1":0, "key2":1})

When doing

test1.c

I get:

{'key1': 0, 'key2': 1}

But how can I get the dictionary value of, lets say, "key1" (only "key1")?

Also, I am trying to add the content of an object to a list (as specific list elements). Is that somehow possible?

I am trying to do something like:

test1_list=[]
test1_list=[].append(Test("text", 0, {"key1":0, "key2":1}))

When I do:

print(test1_list)

I only get:

None

instead of:

["text", 0, {"key1":0, "key2":1}]

3
  • test1.c['key1']. You should review introductionary material like the Python tutorial. Commented Jan 5, 2019 at 11:45
  • So in your second question do you want to add the test1 object or the content of the fields of test1? Commented Jan 5, 2019 at 11:45
  • I want to add the content of the fields of "test1" to the list. Commented Jan 5, 2019 at 12:13

2 Answers 2

1

It will make sense to have a closer look at the python data structures. Your should be able to solve your issues yourself after reading through this documentation.

At least it will help you to better understand the solutions below.


In short, here is how you get the value for a key:

test1.c['key1']
Out[5]: 0

And how to append to a list:

test1_list = []
test1_list.append(Test("text", 0, {"key1":0, "key2":1}))

or in one go:

test1_list = [Test("text", 0, {"key1":0, "key2":1})]
Sign up to request clarification or add additional context in comments.

4 Comments

oh... thank you so much... I will definitely go trough the website you suggested.
My pleasure! And I wish you all the best on your coding adventures!
just one more thing.... when I do ">>> test1_list" I get: [<__main__.Test object at 0x000001EF6CFDA630>], but not the content in form of list elements
@KideW This is normal, as python does not know a priory how to 'show' you the object you have put in the list. You can define the __repr__ method for your class to tell python how to display your object. See docs.python.org/3/reference/datamodel.html#object.__repr__
0

But how can I get the dictionary value of, lets say, "key1" (only "key1")?

Here is a horrible (but, working and dynamic) answer using exec:

In [1]: class Test(object):
            def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self.c = c
            try:
                self.__set_key_values()
            except ValueError:
                pass

            def __set_key_values(self):
                n = 1
                for k in self.c.keys():
                    exec(f'self.key{n} = {self.c[k]}')
                    n += 1

Instance your class with arguments:

In [2]: test1 = Test("text", 0, {"key1":0, "key2":1})

Try returning key values via direct access:

In [3]: test1.key1
Out [3]: 0

In [4]: test1.key2
Out [4]: 1

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.