list.pop() method will raise an exception if list length is zero.
if len(lst) > 0
The use of a while loop inside a for loop can be a tricky thing to manage, but first we should determine if that much logic is even needed. What is the most we need to iterate the list one time?
Answer: A single loop.
As for the textual return, that would never be advisable if the return is expected to be a list. It would be feeding garbage back to the caller and raise an exception as soon as a list method was called on that object. It would certainly be messing up the data, nonetheless.
>>> a = "string" >>> b = a.copy() Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> b = a.copy() AttributeError: 'str' object has no attribute 'copy' >>> b = list(a).copy() >>> b ['s', 't', 'r', 'i', 'n', 'g'] >>>
Here is a list of list methods and built in functions that work on lists, and perhaps other object types, as well.
Method Description Python List append() Add Single Element to The List Python List extend() Add Elements of a List to Another List Python List insert() Inserts Element to The List Python List remove() Removes Element from the List Python List index() returns smallest index of element in list Python List count() returns occurrences of element in a list Python List pop() Removes Element at Given Index Python List reverse() Reverses a List Python List sort() sorts elements of a list Python List copy() Returns Shallow Copy of a List Python List clear() Removes all Items from the List Python any() Checks if any Element of an Iterable is True Python all() returns true when all elements in iterable is true Python ascii() Returns String Containing Printable Representation Python bool() Converts a Value to Boolean Python enumerate() Returns an Enumerate Object Python filter() constructs iterator from elements which are true Python iter() returns iterator for an object Python list() Function creates list in Python Python len() Returns Length of an Object Python max() returns largest element Python min() returns smallest element Python map() Applies Function and Returns a List Python reversed() returns reversed iterator of a sequence Python slice() creates a slice object specified by range() Python sorted() returns sorted list from a given iterable Python sum() Add items of an Iterable Python zip() Returns an Iterator of Tuples