Reputation: 13
I want to know if there's a way that I can change what list an item gets removed from depending on something beforehand.
list0 = ("1", "2", "3")
list1 = ("4", "5", "6")
var = randint(0,1)
if var == 0:
list = "list0"
else:
list = "list1"
list.pop(0)
print(list)
*basically from what I understand, python thinks I'm trying to 'pop' the 1st item from the string "list0//list1" (depending on what's chosen), rather than the list. Is there a way that I can set the variable treat it as one of my list names? Thanks!
Upvotes: 0
Views: 386
Reputation: 31339
There is a difference between a variable name in your program and data.
You can't (easily) match data and variable names from your program.
You can use a mapping to match data:
lists = {0: ["1", "2", "3"],
1: ["4", "5", "6"]}
var = randint(0, 1)
lst = lists[var]
lst.pop(0)
print(lst)
Upvotes: 1
Reputation: 3639
(list1 if var else list0).pop(0)
Mind that list0
and list1
must be lists and not tuples: tuples are immutable. So you should change your code in this way:
list0 = ["1", "2", "3"]
list1 = ["4", "5", "6"]
Upvotes: 0