1

I have a list, not dict, that contains information. When I simply do print my_list it yields:

[<Data_1: name=u'Name of A',id=101>,
<Data_2: name=u'Name of B',id=102>,
<Data_3: name=u'Name of C',id=103>,
...]

When I do for x in my_list: print x it yields:

Name of A
Name of B
Name of C
...

Clearly Python is parsing this somehow but how would I extract both the name and id portions of each element? If this was in a dict, I would do my_list.iteritems but I am a little stuck on how to do this with just a normal list.

Thanks

3
  • 2
    <Data_1: name=u'Name of A',id=101> definitely a queryset. So x.name and x.id should work. Commented Apr 27, 2015 at 16:20
  • i can't believe it was that easy. thanks Commented Apr 27, 2015 at 16:21
  • 2
    Incidentally, your two pieces of code produce different output because printing an object directly invokes its __str__ method, whereas printing a list containing objects invokes their __repr__ methods. Commented Apr 27, 2015 at 16:44

1 Answer 1

2

Solution to your question would be:

for x in my_list: print x.name, x.id
Sign up to request clarification or add additional context in comments.

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.