How to check if any items in a list is in another list

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
User avatar
GeeSeki
Regular
Posts: 109
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

How to check if any items in a list is in another list

#1 Post by GeeSeki » Sun Oct 16, 2022 11:36 pm

What's the best/most efficient way to check if ANY of the items in 'char_gift_like' list is in the category list for 'gift_item1'?

For example: they both include "gaming" so therefore, the character will accept the gift.

Code: Select all

default gift_item1 = {
"id":"items_item1",
"name":"Item #1",
"description":"The first item.",
"category":["gaming","young","anime","music","blue","decor"],
"price":1500,}

Code: Select all

default char_gift_like = ["gaming","young","toy","food","street","comfy","white","pink","blue"]

User avatar
Tess
Newbie
Posts: 23
Joined: Thu Aug 04, 2022 3:43 pm
Projects: The Songbird Guild
Organization: Yurisoft
Github: wainwt2
Discord: Tess#7782
Contact:

Re: How to check if any items in a list is in another list

#2 Post by Tess » Mon Oct 17, 2022 12:00 am

I haven't tested this since I'm on my phone, but I think this'll work:

Code: Select all

If [category for category in char_gift_like if category in gift_item1["category"]]:
    #They like your gift! :)
else:
    #They think it's ok...
Empty lists will be read as False in if statements, so you can just check it directly.

User avatar
GeeSeki
Regular
Posts: 109
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: How to check if any items in a list is in another list

#3 Post by GeeSeki » Mon Oct 17, 2022 12:19 am

Tess wrote:
Mon Oct 17, 2022 12:00 am
I haven't tested this since I'm on my phone, but I think this'll work:

Code: Select all

If [category for category in char_gift_like if category in gift_item1["category"]]:
    #They like your gift! :)
else:
    #They think it's ok...
Empty lists will be read as False in if statements, so you can just check it directly.
Thanks! That worked

Post Reply

Who is online

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