my_list = ["zero", "wow", "peach", 3, 4, "nice", "pickle"]
I want to print "wow", "nice", "peach"
So:
my_list[1]
my_list[5]
my_list[2]
How can I do this in one line, or at-least quicker than the above?
You can use a list comprehension to return a list of values:
[my_list[i] for i in [1, 5, 2]]
Or to print one by one:
for i in [1, 5, 2]:
print(my_list[i])
or as a 1-liner using the argument unpacking (*
) operator to "flatten" a generator:
print(*(my_list[i] for i in [1, 5, 2]), sep='\n')