Rmor91
Rmor91

Reputation: 262

Retrieving from list of objects by id

In the following code I create a list of three objects and use the variable a,b,c to store the first, second and then the third object by their id's but when I try to store the third object in variable c, it stores a list of the second and third object.

class Obj1():
    id='obj1'

class Obj2():
    id='obj2'

class Obj3():
    id='obj3'

list1=[Obj1(),Obj2(),Obj3()]

a=list1[id=="obj1"]
print a

b=list1[id!='obj1']
print b

c=list1[id!='obj1'and id!='obj2':]
print c

When I run this code I get :

<__main__.Obj1 instance at 0x02AD5DA0>
<__main__.Obj2 instance at 0x02AD9030>
[<__main__.Obj2 instance at 0x02AD9030>, <__main__.Obj3 instance at 0x02AD90A8>]

why does variable c contain two objects?

Upvotes: 4

Views: 6702

Answers (4)

Stephen Lin
Stephen Lin

Reputation: 4912

id!='obj1'and id!='obj2' returns true which equals 1 in Python, that is to say, c=list1[id!='obj1'and id!='obj2':] equals c=list1[1:] , which of course has two objects.

BTW, id is the name of a built-in function. Please avoid using it as a name of varible.

Upvotes: 2

JKillian
JKillian

Reputation: 18351

Using a dictionary is probably the best idea in this case, as mentioned by Medhat. However, you can do things in a similar way to what you attempted using list comprehensions:

a = [e for e in list1 if e.id == "obj1"]
print a

b = [e for e in list1 if e.id != "obj1"]
print b

c = [e for e in list1 if e.id != "obj1" and e.id != "obj2"]
# Or:
# [e for e in list1 if e.id not in ("obj1", "obj2")]
print c

Upvotes: 4

Medhat Gayed
Medhat Gayed

Reputation: 2813

You should use a dictionary instead:

obj1 = Obj1()
obj2 = Obj2()
obj3 = Obj3()
list1 = {obj1.id: obj1, obj2.id: obj2, obj3.id: obj3}

Then access your objects like this:

a = list1[obj1.id]
b = list1[obj2.id]
c = list1[obj3.id]

Upvotes: 2

James Sapam
James Sapam

Reputation: 16940

Your list1 contains 3 elements:

>>> list1
[<__main__.Obj1 instance at 0x103437440>, <__main__.Obj2 instance at 0x1034377a0>,         <__main__.Obj3 instance at 0x103437320>]
>>> id!='obj1'
True
>>> id!='obj2'
True
>>> True and True
True
>>> list1[True:]
[<__main__.Obj2 instance at 0x1034377a0>, <__main__.Obj3 instance at 0x103437320>]
>>>

True is 1 and False is 0 index:

Here is the example:

>>> ls = [1,2]
>>> ls[True]
2
>>> ls[False]
1
>>>

So, list[True:] is equal to list[1:] which is from first element till last.

In your case the the last two elements in the list1.

Upvotes: 1

Related Questions