Mel Mel - 4 months ago 15
Python Question

How do I put variable in a list slicing operation?

I have a list in a function, and I want to sometimes use the whole thing, sometimes just a section depending on an offset variable. Sometimes I want to apply the offset to the start, sometimes the end.

I know

my_list = my_list[:]


will give me just the same list, and

start= None
end = None
my_list[start:end]


seems to do the same thing.

But the trouble is that sometimes I want to increment the parameter but I can't add 1 to None. (The start seems less of a problem as I can initialize it to 0.)

I would like to have something like:

my_list[start: end + offset]


And sometimes I even want a constant stuck in there too, such as

my_list[start: end -1 + offset]


I am pretty tired as I write this so apologies if this is easy.

Edit - my final solution....

I followed the idea of using the length in the end parameter (as described in the solution) combined with the idea of changing my offset variable into two variables. Details are below...

The whole idea was that I had a function that compares elements in two lists. The offset is zero by default and so the first element in one is compared with the first element of the other. Then I wanted to have an offset so the first element in one is compared with the second element of the other (or third, etc)

Because I wanted the offset to be used differently in different places, I found I had to process the offset into two variables beforehand...

if offset < 0:
neg_offset = abs(offset)
if offset > 0:
pos_offset = offset


And then I put the two lists in my function like this: When the lists are the same length...

get_list_comparison(my_first_list[pos_offset: len(my_first_list)-neg_offset], my_second_list[neg_offset: len(my_second_list)-pos_offset])


When the second list is shorter than the first list:

get_list_comparison(my_first_list[pos_offset:len(my_first_list)-1-neg_offset], my_second_list[neg_offset: len(my_second_list)-pos_offset])


When the lists are uneven, I suppose I could have worked out the difference computationally rather than hardcoding the -1 (or -2 etc) but this was good enough to work for me.

Answer

You can't sum None. However, you can sum to other numbers like 0 (start) and len(my_list) (End)

my_list = [1,2,3,4,5,7,8,9,10]
print(my_list)

my_list = my_list[:]
print(my_list)

start1 = None
end1 = None
my_list = my_list[start1:end1]
print(my_list)


######################################
start2 = 0
end2 = len(my_list)
my_list = my_list[start2:end2]
print(my_list)

my_list = my_list[start2 + 3:end2 - 3]
print(my_list)

(First four outputs are just the same, to show the difference and how it works).

That way you can change the parameters easily. Because start is just '0', and the same as 'None' but because of it's datatype you can increase and decrease it's value. and end is len(my_list) which returns an integer representing the length of your list, and thus, also the end. Because it's an integer, you can also increase/decrease that value.

Another thing you can do is using negatives:

#####################################
###Using negatives
start3 = -3
end3 = +3
my_list = my_list[start3:end3]
print(my_list)

See? I kind of 'reversed' start end end, by making end positive, and start negative.

Output of all code:

[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[4, 5, 7]
[4, 5, 7]