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
Related
This question already has answers here:
Why can I use a list index as an indexing variable in a for loop? [duplicate]
(6 answers)
Are for-loop name list expressions legal?
(2 answers)
Closed last year.
So in a quiz, I have given the question what the execution of this block of code gives (The one under).
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])
I had selected error but to my surprise, it actually works and is able to print all the elements inside the list. But how?
firstly the element getting variable (usually i) is shadowing the actual variable on top of that we are getting the first element inside a number so how is it working.
a[0] is type of a so it can be used to iterate over the array but as in look you are assigning value in a[0] it's value will change to last element of the array.
a = [0,1,2,3]
for a[0] in a:
print(a[0]
Will result in:
0
1
2
3
But now printing a will give you modified array:
print(a)
[3, 1, 2, 3]
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.
This question already has answers here:
Access multiple elements of list knowing their index [duplicate]
(11 answers)
Closed 5 years ago.
Say I have a list:
[0,1,2,3,4,5,6,7,8,9]
From this list I want to retrieve the centre elements at positions 4, 5 and 6. So: [4,5,6] is the output I want from this list.
I know how to remove elements, that's easy using this method:
x = [position1:] + [:position2]
But I don't know how to find those items.
Use a slice:
l = [0,1,2,3,4,5,6,7,8,9]
print(l[4:7]) # [4, 5, 6]
This question already has answers here:
How do I get the number of elements in a list (length of a list) in Python?
(11 answers)
Closed 8 years ago.
For example, list[1, 2, 3] has 3 items (and indexex 0, 1, 2).
How can I print the number of objects in a list? In this istance, it would be "3".
use len :
>> lst = [0,1,2,3]
>> len(lst)
4
Note : never use list as a variable name.
This question already has answers here:
Why does substring slicing with index out of range work?
(3 answers)
Closed 9 years ago.
While this code will raise indexError:
In [1]: lst = [1, 2, 3]
In [2]: lst[3]
IndexError: list index out of range
Slicing the list with "out of range index" will not produce any error.
In [3]: lst[3:]
Out[3]: []
What is the rationale of this design?
When you are accessing an element in a list whose index is beyond its length, we cannot return anything. (There is no way we can represent an element which is not there). That's why the error is thrown. But when you are slicing, you are making a sliced COPY of the original list and that new list can be empty if the start or end are not valid.
It's nice being able to test if an element exists:
if sys.argv[2:]:
# do something with sys.argv[2], knowing it exists