Via trial and error I found out that
my_list = range(10)
my_list[:None] == my_list[:]
I use this for django query sets so I can define a size or take all:
some_queryset[:length if length else None]
# @IanAuld
some_queryset[:length or None]
# @Bakuriu
# length works for all numbers and None if you want all elements
# does not work with False of any other False values
some_queryset[:length]
- Is this good practice to use
Nonewhile slicing? - Can problems occur with this method in any case?
length = 0(asking for a queryset of length 0), you will receive quite an unexpected result by getting everything. - kylieCattmy_list[None:None] == my_list[:]isTrue! - Gurupad Hegdelengthinstead oflenght. - Kevin0will never be an acceptable value forlengththen it should work fine. Although it can be shortened tolst[:length or None]- kylieCatt