user9153055
user9153055

Reputation: 19

Check if a value exists on a list

I have a list: object = [x, y, type, name]

I want to add it to an array, if it's not already added:

if not object in world[int(room)]:
   world[int(room)].append(object)

This worked very nice without the x and y coordinates.

What I want to do is to now compare the lists without x and y. Just by type and name.

Something like if not object[2:] in world[room][2:]

Upvotes: 0

Views: 1158

Answers (1)

Joe Iddon
Joe Iddon

Reputation: 20434

Create a generator of lists made from the elements from index 2 onwards for each item in the world[int(room)] list:

if not object[2:] in (i[2:] for i in world[int(room)]):
   world[int(room)].append(object)

N.b. you should also not use object as it is an identifier that refers to a built-in type, so by using it as a variable name, you are overriding it.

Upvotes: 2

Related Questions