@@ -348,6 +348,33 @@ def select(self, **kwargs: list[Any]) -> tuple[list[Any]]:
348348 return result_true , result_false
349349
350350
351+ class DataListFirst (ComfyNodeABC ):
352+ """
353+ Returns the first element in a list.
354+
355+ This node takes a list as input and returns the first element of the list.
356+ If the list is empty, it returns None.
357+ """
358+ @classmethod
359+ def INPUT_TYPES (cls ):
360+ return {
361+ "required" : {
362+ "list" : (IO .ANY , {}),
363+ }
364+ }
365+
366+ RETURN_TYPES = (IO .ANY ,)
367+ RETURN_NAMES = ("first_element" ,)
368+ CATEGORY = "Basic/Data List"
369+ DESCRIPTION = cleandoc (__doc__ or "" )
370+ FUNCTION = "get_first_element"
371+ INPUT_IS_LIST = True
372+
373+ def get_first_element (self , ** kwargs : list [Any ]) -> tuple [Any ]:
374+ input_list = kwargs .get ('list' , [])
375+ return (input_list [0 ] if input_list else None ,)
376+
377+
351378class DataListGetItem (ComfyNodeABC ):
352379 """
353380 Retrieves an item at a specified position in a list.
@@ -453,6 +480,33 @@ def insert(self, **kwargs: list[Any]) -> tuple[list[Any]]:
453480 return (result ,)
454481
455482
483+ class DataListLast (ComfyNodeABC ):
484+ """
485+ Returns the last element in a list.
486+
487+ This node takes a list as input and returns the last element of the list.
488+ If the list is empty, it returns None.
489+ """
490+ @classmethod
491+ def INPUT_TYPES (cls ):
492+ return {
493+ "required" : {
494+ "list" : (IO .ANY , {}),
495+ }
496+ }
497+
498+ RETURN_TYPES = (IO .ANY ,)
499+ RETURN_NAMES = ("last_element" ,)
500+ CATEGORY = "Basic/Data List"
501+ DESCRIPTION = cleandoc (__doc__ or "" )
502+ FUNCTION = "get_last_element"
503+ INPUT_IS_LIST = True
504+
505+ def get_last_element (self , ** kwargs : list [Any ]) -> tuple [Any ]:
506+ input_list = kwargs .get ('list' , [])
507+ return (input_list [- 1 ] if input_list else None ,)
508+
509+
456510class DataListLength (ComfyNodeABC ):
457511 """
458512 Counts the number of items in a list.
@@ -588,6 +642,38 @@ def pop(self, **kwargs: list[Any]) -> tuple[list[Any], Any]:
588642 return result , None
589643
590644
645+ class DataListPopRandom (ComfyNodeABC ):
646+ """
647+ Removes and returns a random element from a list.
648+
649+ This node takes a list as input and returns the list with the random element removed
650+ and the removed element itself. If the list is empty, it returns None for the element.
651+ """
652+ @classmethod
653+ def INPUT_TYPES (cls ):
654+ return {
655+ "required" : {
656+ "list" : (IO .ANY , {}),
657+ }
658+ }
659+
660+ RETURN_TYPES = (IO .ANY , IO .ANY )
661+ RETURN_NAMES = ("list" , "item" )
662+ CATEGORY = "Basic/Data List"
663+ DESCRIPTION = cleandoc (__doc__ or "" )
664+ FUNCTION = "pop_random_element"
665+ INPUT_IS_LIST = True
666+ OUTPUT_IS_LIST = (True , False )
667+
668+ def pop_random_element (self , ** kwargs : list [Any ]) -> tuple [list [Any ], Any ]:
669+ from random import randrange
670+ input_list = kwargs .get ('list' , []).copy ()
671+ if input_list :
672+ random_element = input_list .pop (randrange (len (input_list )))
673+ return input_list , random_element
674+ return input_list , None
675+
676+
591677class DataListRemove (ComfyNodeABC ):
592678 """
593679 Removes the first occurrence of a specified value from a list.
@@ -864,13 +950,16 @@ def convert(self, **kwargs: list[Any]) -> tuple[set[Any]]:
864950 "Basic data handling: DataListExtend" : DataListExtend ,
865951 "Basic data handling: DataListFilter" : DataListFilter ,
866952 "Basic data handling: DataListFilterSelect" : DataListFilterSelect ,
953+ "Basic data handling: DataListFirst" : DataListFirst ,
867954 "Basic data handling: DataListGetItem" : DataListGetItem ,
868955 "Basic data handling: DataListIndex" : DataListIndex ,
869956 "Basic data handling: DataListInsert" : DataListInsert ,
957+ "Basic data handling: DataListLast" : DataListLast ,
870958 "Basic data handling: DataListLength" : DataListLength ,
871959 "Basic data handling: DataListMax" : DataListMax ,
872960 "Basic data handling: DataListMin" : DataListMin ,
873961 "Basic data handling: DataListPop" : DataListPop ,
962+ "Basic data handling: DataListPopRandom" : DataListPopRandom ,
874963 "Basic data handling: DataListRemove" : DataListRemove ,
875964 "Basic data handling: DataListReverse" : DataListReverse ,
876965 "Basic data handling: DataListSetItem" : DataListSetItem ,
@@ -893,13 +982,16 @@ def convert(self, **kwargs: list[Any]) -> tuple[set[Any]]:
893982 "Basic data handling: DataListExtend" : "extend" ,
894983 "Basic data handling: DataListFilter" : "filter" ,
895984 "Basic data handling: DataListFilterSelect" : "filter select" ,
985+ "Basic data handling: DataListFirst" : "first" ,
896986 "Basic data handling: DataListGetItem" : "get item" ,
897987 "Basic data handling: DataListIndex" : "index" ,
898988 "Basic data handling: DataListInsert" : "insert" ,
989+ "Basic data handling: DataListLast" : "last" ,
899990 "Basic data handling: DataListLength" : "length" ,
900991 "Basic data handling: DataListMax" : "max" ,
901992 "Basic data handling: DataListMin" : "min" ,
902993 "Basic data handling: DataListPop" : "pop" ,
994+ "Basic data handling: DataListPopRandom" : "pop random" ,
903995 "Basic data handling: DataListRemove" : "remove" ,
904996 "Basic data handling: DataListReverse" : "reverse" ,
905997 "Basic data handling: DataListSetItem" : "set item" ,
0 commit comments