[Solved]How can I check if list contains an item?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Doeny
Regular
Posts: 26
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

[Solved]How can I check if list contains an item?

#1 Post by Doeny » Sat Oct 08, 2022 2:24 pm

So I made a code that removes an item from the list, but I have problem checking if it's still in. Any ideas how to fix it?
This removes the item (works fine (i hope))

Code: Select all

$ renpy.random.shuffle(list)
$ list1 = lsit.pop() 
And this checks if item's not in the list:

Code: Select all

imagebutton:
	if list != "item_1":
      		idle "gallery_img_1"
   		action Jump("gallery_img_1_action")
        else:
                idle "empty_image"
                action NullAction()
Last edited by Doeny on Sun Oct 09, 2022 9:22 am, edited 1 time in total.

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1882
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How can I check if list contains an item?

#2 Post by Ocelot » Sat Oct 08, 2022 5:27 pm

Code: Select all

#checks if item is in list:
if item in list:
    pass
else:
    pass

# check if item not in list
if item not in list:
    pass
If item is an instance of your own class without defined comparison operators, you can run into identity vs equality problem, but for built-in types it should work fine.
< < insert Rick Cook quote here > >

Doeny
Regular
Posts: 26
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: How can I check if list contains an item?

#3 Post by Doeny » Sun Oct 09, 2022 9:21 am

Ocelot wrote:
Sat Oct 08, 2022 5:27 pm

Code: Select all

#checks if item is in list:
if item in list:
    pass
else:
    pass

# check if item not in list
if item not in list:
    pass
If item is an instance of your own class without defined comparison operators, you can run into identity vs equality problem, but for built-in types it should work fine.
Yup, works fine. Thanks!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]