if list is empty [solved]

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
lovebby
Regular
Posts: 106
Joined: Mon Sep 09, 2013 12:24 am
Deviantart: lovebby
Contact:

if list is empty [solved]

#1 Post by lovebby »

So I'm essentially trying to get this code working.

Code: Select all

$ list = ['apples','oranges']

"I'm gonna making apple sauce and orange juice."
$list.remove("apples")
$list.remove("oranges")
"The list is empty."
if list == []:
    "I need to buy apples and oranges."
Is there a way to check if the list is empty?
Last edited by lovebby on Fri Apr 24, 2020 2:08 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: if list is empty

#2 Post by hell_oh_world »

lovebby wrote: Tue Apr 21, 2020 5:42 am So I'm essentially trying to get this code working.

Code: Select all

$ list = ['apples','oranges']

"I'm gonna making apple sauce and orange juice."
$list.remove("apples")
$list.remove("oranges")
"The list is empty."
if list == []:
    "I need to buy apples and oranges."
Is there a way to check if the list is empty?
technically, python treats its data types as booleans (True or False)... python magically understands when to consider a data type False or True...
For list you can do that like this...

Code: Select all

default my_list = []
label start:
	if my_list: # this is equivalent to `my_list == []`, you can reverse the condition by setting it to `not my_list`, which is equal to `my_list != []`
		"Your list has one or more elements."
	else:
		"Your list is empty."

AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Re: if list is empty

#3 Post by AJFLink »

lovebby wrote: Tue Apr 21, 2020 5:42 am So I'm essentially trying to get this code working.

Code: Select all

$ list = ['apples','oranges']

"I'm gonna making apple sauce and orange juice."
$list.remove("apples")
$list.remove("oranges")
"The list is empty."
if list == []:
    "I need to buy apples and oranges."
Is there a way to check if the list is empty?

Code: Select all

$ test_list = ['apples','oranges']

"I'm gonna making apple sauce and orange juice."
$ del test_list[0]
$ del test_list[0]
"This list is empty"
if len(test_list) == 0:
	"I need to buy apples and oranges."
else:
	"This list is not empty."
If you want to have a function that you can use to see if a list/tuple is empty:

Code: Select all

python:
	def isEmptyList(your_list):
			if len(your_list) == 0:
				return True
			return False 
I hope that this helps.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: if list is empty

#4 Post by Imperf3kt »

More useful would be checking if the item is in the list, rather than if the list is empty. You might have oranges, but no apples. If you try to make apple juice, you can, despite having no apples, because the list isn't empty.

I learnt how to use python lists here.
https://www.w3schools.com/python/python_lists.asp

An example of how to check if an item is in a list can be found here.
https://www.w3schools.com/python/trypyt ... mo_list_in
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
lovebby
Regular
Posts: 106
Joined: Mon Sep 09, 2013 12:24 am
Deviantart: lovebby
Contact:

Re: if list is empty

#5 Post by lovebby »

Thank you everyone It got it now! And thanks imperfect for that link, there's lots ofinformation thats gonna help me out there haha.

Post Reply

Who is online

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