You're on the right track.
if exporterslist: #if empty_list will evaluate as false.
importer = exporterslist.pop(0)
else:
#Get next entry? Do something else?
Answer from NightShadeQueen on Stack OverflowYou're on the right track.
if exporterslist: #if empty_list will evaluate as false.
importer = exporterslist.pop(0)
else:
#Get next entry? Do something else?
This one..
exporterslist.pop(0) if exporterslist else False
..is somewhat the same as the accepted answer of @nightshadequeen's just shorter:
>>> exporterslist = []
>>> exporterslist.pop(0) if exporterslist else False
False
or maybe you could use this to get no return at all:
exporterslist.pop(0) if exporterslist else None
>>> exporterslist = []
>>> exporterslist.pop(0) if exporterslist else None
>>>
Update:
A funkier solution only for those with wanderlust or just for fun:
>>> exporterslist = []
>>> importer = exporterslist and exporterslist.pop(0) or False
>>> importer
False
>>> exporterslist = ['a']
>>> importer = exporterslist and exporterslist.pop(0) or False
>>> importer
'a'
IndexError: pop from empty list
Annoying "IndexError: pop from empty list"
return None or not to return None
Might not be exactly on topic but I wish more people would code their functions to NOT return None and instead return expected type.
BeautifulSoup is the biggest perpetrator of this. Whenever you parse the tree/soup you'll either get a list of results or a single result, now if no results are found instead of getting an empty list you get None returned, which in turn requires you to make checks on every single lookup which really messes up your code up and makes everything look ridiculous.
switched to lxml/xpath and never looked back.
More on reddit.comHow to remove empty dictionary value from list?
Yes the quotes (empty string) are considered values and you would use an if statement. Probably what I would suggest is a list comprehension:
[d for d in mylist if d['name']]
This creates a new list with each dictionary value in there if the value corresponding to the key 'name' is not an empty string. The empty string is a Falsey value in Python, so instead of checking if d['name'] != '' (which is also totally valid) you can just ask if d['name'].
Hello everyone, total python brainlet here, I have been stuck at this problem for hours on end and I seem to never get rid of it. The objective of the code was for the pop() function to stop at the specific number written below but it doesn't. It just keeps going until it encounters this error.
Traceback (most recent call last):
File "<pyshell#112>", line 11, in <module>
s.pop()
File "<pyshell#109>", line 9, in pop
return self.items.pop()
IndexError: pop from empty listThis is the main code, I have run out of ideas and I have no clue on how to accomplish this without succumbing to frustration. I would greatly appreciate if anyone would give feedback as to how one could solve this.
while True:
c = 1
l = 10
print("What do you want to do with the stack?")
uinput = int(input("Choices: 1=Push, 2=Pop, 3=Display, 4 =Quit"))
#fine
if uinput == 1:
c+= 1
s.push(input("Enter want you want to add to the stack."))
#error cause
elif uinput == 2:
while l != 0:
l= c - 1
print(s.pop())
#fine
elif uinput == 3:
if c >= 1:
print(s.peek())
else:
print("Please push")
#fine
elif uinput == 4:
import sys
sys.exit()Additional code, if it would help.
class Stack: def __init__(self): self.items =[] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1]