Reputation: 73
Im basically need to be able to show or hide an item in a list
such that when i choose an option an item is shown if it's hidden such as below
a = ['A','B','C','D','E','F','G','H','I']
def askChoice():
choice = 0
if choice == 1:
a[-1] = X ##Therefore last item in the list is hidden
elif choice == 2:
a[-1] = a[-1] ##Therefore item shown
else:
a[-1] = [] ## There an empty placeholder where any other item can be placed
return choice
Upvotes: 0
Views: 5509
Reputation: 1474
You need to store the information about which items in the list are shown or hidden.
I would do something like:
a = [['A',True], ['B',True], ['C',True], ['D',True], ['E',True]]
def show(index):
a[index][1] = True
def hide(index):
a[index][1] = False
def display():
print([x[0] for x in a if x[1]])
There are other methods, but storing the info in your list means you won't run into confusing bugs where your data on what to show and what not to does not match up with your actual printable data. It also ensures you will have to update show/hide data when you update the list, which otherwise could be easily overlooked.
Upvotes: 3