Him
Him

Reputation: 5551

How to create a slice object that selects all elements?

I am building up slices programatically for reasons, and would like to create a slice that grabs "everything". My immediate intuition was that

my_slice = slice() 

would produce an object such that

assert (my_list[my_slice] == my_list) #True

However, a quick look at the docs reveals that slice requires a stop parameter.

Can I obtain an object all_slice such that any_list[all_slice] == any_list for all any_list?

Upvotes: 5

Views: 649

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61910

You can pass None:

my_list = [1, 2, 3, 4]
my_slice = slice(None)
print(my_list[my_slice])

Output

[1, 2, 3, 4]

Upvotes: 12

Related Questions