0

Can someone guide me how to extract the attribute of object that is inside a list of list. For example, I got:

class Symbol:
    def __init__(self, name, mtype, value = None):
        self.name = name
        self.mtype = mtype
        self.value = value


# ... some more code

Then after running some code, I will get list of variable such as:

list_var = [Symbol(varname_1, vartype_1), Symbol(varname_2, vartype_2), ..., Symbol(varname_n, vartype_n)]

So, I would like to get only the vartype as the list of vartype i.e

list(vartype_1,...vartype_n)
1
  • 1
    use [obj.mtype for obj in list_var] Commented Jan 1, 2019 at 4:23

2 Answers 2

1

You can get the attribute of any object by doing object.attribute, or in your case Symbol().mtype

The fact that it's in a list makes no difference. Iterate over the list and grab the attribute for each element:

n_lst = [symbol.mtype for symbol in list_var]
Sign up to request clarification or add additional context in comments.

Comments

0

Or use map:

n_lst = list(map(lambda x: x.mtype,list_var))

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.