This question already has answers here:
Pipe character in Python
(6 answers)
Closed 3 years ago.
In python slice notation for lists and tuples, I noticed that a pipe character doesn't throw an error. I'm not sure what exactly it does though, as the results seem a little random.
testA = [1,2,3,4,5,6,7,8,9]
Given that
testA[0:3]
gives
[1, 2, 3]
and
testA[4:6]
gives
[5, 6]
What does this pipe do?
testA[0:3|4:6]
it gives
[1, 7]
Any ideas?
testA[0:3|4:6]
evaluates as
testA[0 : (3 | 4) : 6]
Which, in turn, evaluates via bitwise-or to
testA[0 : 7 : 6]
And this corresponds to the range 0 : 7 with a step size of 6. Hence only the first and last index are used.
Related
This question already has answers here:
Why does substring slicing with index out of range work?
(3 answers)
Python index out of range in list slicing [duplicate]
(2 answers)
Why does assigning past the end of a list via a slice not raise an IndexError? [duplicate]
(2 answers)
Closed 2 years ago.
Given the expressions:
>>> l = [1,2,3]
>>> l[10:20] = [4,5]
>>> l
[1, 2, 3, 4, 5]
Why doesn't Python nag about the nonexistent items meant to be deleted? l.remove(55) does raise a ValueError.
l.remove(55) Raises an error because you don't have the value 55 in your list.
On the other hand, l[10:20] = [4,5] is not crashing your code because that slicing method will try to add elements on those indexes, if it can't, it will be adding the new elements in the last position of your array.
Also, if you try to do (for example) l[10]=10, this will Raise the exception: IndexError: list index out of range
This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
Why for example when slicing a list with square brackets in this way
spam = [1, 2, 3]
When we print(spam[0:2]) the 3 is not typed while the index of that is 2?
This style of indexing is just very beautiful and comfortable. There're lots of cases where this can be useful. The easiest one is when you have to do something on two halves of an array:
spam = [1, 2, 3, 4, 5]
center_index = len(spam) / 2
func(spam[center_index:])
func(spam[:center_index])
In this example, you don't have to think about +1/-1
Because Python slicing is inclusive # start and exclusive # the end
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 5 years ago.
A = [2,4,6,8,10,12]
for a in A:
if a%2 == 0: # If 2 divides a, remove a from A
A.remove(a)
print(A)
When I execute this block of code, the console output is [4,8,12].
My understanding of this code is that if any of the elements in [A] are divisible by 2, then we remove them from the list. In the list above, all elements are in fact divisible by 2, but only 2, 6, and 10 were removed. Anyone care to explain why 4, 8, and 12 were not removed?
Removing elements from a list while you're iterating through it messes up the iteration. You should use the filter function or a list comprehension instead.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
I'm a beginner attempting to learn Python. I am familiarising myself with the list data type; I've defined the following list:
>>> numbers = [1, 2, 3, 4]
Typing:
>>> numbers[0]
1
>>> numbers[1]
2
>>> numbers[2]
3
>>> numbers[3]
4
Given this, why do I get the following when I attempt to retrieve the following list elements:
>>> numbers[0:3]
[1, 2, 3]
Why isn't the list element '4' included in the response from the interpreter?
Thank you for your help.
Slice notation does not include the last element (similar to the range() function in that respect). If you want to include the last element, simply omit an index. Also, the default start is the beginning, so you don't need 0 there either:
>>> numbers[:]
[1, 2, 3, 4]
Note that this is a (shallow) copy of numbers. If you save a reference to it, you can mutate it without affecting the original numbers.
That's how slicing works in Python. To quote a tutorial:
Note how the start is always included, and the end always excluded.
This makes sure that s[:i] + s[i:] is always equal to s.
The example uses a string, but slicing works the same way with lists.
numbers[0:3] list from 0 up to 3 but 3 is excluded (like range(0,3))
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
There is this code:
a = [1, 2, 3, 4, 5]
a[2:4:-1] # returns []
a[4:2:-1] # returns [5, 4]
Why statement a[2:4:-1] returns an empty list altough the range is specified?
If you attempt to use a[2:4:-1] you try to go backward from list index 2 to 4 which will obviously not work.