Python: pop from empty list
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?
Python: pop from empty list?
Your approach wasn't working because index goes out of range instead you could do the following code to read and sort the numbers present in the file.
from sys import argv
with open(argv[1],'r') as myfile:
for line in myfile:
print (sorted(map(int, line.split(','))))
If your file contains comma seperated values.
Pop from Empty List error during Black Jack
Ok, so thanks to Jeff H for pointing out that the call for the card deal was inside an infinite loop, even though I couldn't fix the problem by breaking the loop itself, I did figure out how to start off the initial deal by absorbing it into the game_check
statement above, like so:
while game_on == True:
game_check = input('Would you like to play Black Jack, Y/N? ').lower()
if game_check == 'y':
round_counter = 0
dealer_name = 'Klaus the Dealer'
dealer = Dealer(dealer_name)
player_name = input("Player One, what is your name? ")
player_one = Player(player_name)
print(f"Nice to meet you {player_name}, you'll be playing against {dealer_name}\n")
print(player_one)
print(dealer)
print("Let's play Black Jack!\n")
playing = True
for x in range(2):
player_one.add_cards(game_deck.deal_one())
dealer.add_cards(game_deck.deal_one())
This won't account for having to deal cards into the hand later while in another while
loop, but it will fix the first issue.
Pop from empty list
By default, the timeit
method runs a million times. However, your list is only 10000 items long. So when timeit
runs, the first 10000
times will happily pop, but the 10001
iteration will fail. You can set the number of times explicitly:
popz.timeit(n=10000)
but ... this is a really tricky timing situation since you want to time how long it takes to .pop
, but the size of your list is constantly changing. If you're looking for the asymptotic performance, you probably could verify that .pop(0)
has asymptotic performance of O(N). However concrete times are going to be hard to come by since you either have to time the amount of time it takes to create the list along with the .pop
, or you're list is constantly changing while you're timing it. Neither of these is ideal ... But I guess that's the problem with trying to time non-idempotent methods...
Pop from Empty list error
I suspect maybe your indentation is wrong for InsertIntostacks(lst1)
, and that's the problem.
Try ensuring that InsertIntostacks(lst1)
is properly aligned with the for
loop, meaning it executes after the loop, not within it. Right now it's executing during every iteration of the loop, including the first one, where lst
is indeed empty.
Related Topics
Splitting a List Based on a Delimiter Word
How to Implement Server Push in Flask Framework
Python Nested Functions Variable Scoping
What Is the Most Pythonic Way to Pop a Random Element from a List
How to Save an Image Locally Using Python Whose Url Address I Already Know
How to Filter Rows in Pandas by Regex
Example Use of "Continue" Statement in Python
"Pythonic" Method to Parse a String of Comma-Separated Integers into a List of Integers
Cannot Install Lxml on MAC Os X 10.9
Numpy Index Slice Without Losing Dimension Information
How to Clamp an Integer to Some Range
Django Rest Framework File Upload
Backporting Python 3 Open(Encoding="Utf-8") to Python 2
Django: Improperlyconfigured: the Secret_Key Setting Must Not Be Empty