This question already has answers here:
Logical indexing with lists
(3 answers)
Closed 3 years ago.
I have no idea how this code works and it returns an output of the first element in the list.
I have tried with different inputs since though I'm getting all the same output.
[1,2,3][bool("")]
Since bool("")==False. Also, False is 0. Therefore [1,2,3][bool("")] is same as [1,2,3][0] which return first element of list ultimately.
bool("")==0
This is because "" is regarded as false. This means your statement will be,
[1,2,3][0]
which returns the first element of the list. In this case
1
Source: https://www.programiz.com/python-programming/methods/built-in/bool
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
First time using StackOverflow, apologies in-case of any issues.
In python,
list[::-1] returns the reversed list
list[0:len(list)+1] returns the complete list
So why list[0:len(list)+1:-1] returns an empty list?
Further, for a list l= [0,1,2,3,4,5], if I want like [4,3,2]:
Trying l[2:5:-1], returns an empty list. But l[2:5][::-1] works.
Can anyone explain why this is happening? Or what is python actually doing when we slice a list, with a value for step?
Thanks in advance :)
some_list[start:stop:step] means give me elements starting at start and incrementing by step until you hit stop.
Well, if you start at 0 and increment by -1, you never actually reach the stop.
This question already has answers here:
list.index() function for Python that doesn't throw exception when nothing found
(9 answers)
Finding the index of an item in a list
(43 answers)
Closed 1 year ago.
This works:
def find_file_ix(fn,fn_list):
if fn in fn_list:
ix=fn_list.index(fn)
else:
ix=-1
return ix
l=['a','b','c','d','e']
print(find_file_ix('d',l))
print(find_file_ix('z',l))
and prints 3, -1 as expected.
I could accomplish the same thing with a try/except sequence. However, I find both clumsy and the above code seems to have an element of redundancy - surely the 'in' test must have already found the index?
This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
I am very new to Python and I encountered one issue that I cannot understand. Let say I have string variable:
myVar = "abcdefgh"
I want to display it backward, no problem:
print(myVar[::-1])
and I get hgfedcba. Nothing surprising here. I should get the same with this somewhat verbose code:
print(myVar[len(myVar)-1:0:-1])
but this time the result is hgfedcb. Then I have tried not to subtract 1 from len(myVar) and the result was exactly the same. I do not understand why, especially that lines:
print(myVar[::1])
print(myVar[0:len(myVar):1])
display the same results.
So, my question is why print(myVar[len(myVar):0:-1]) does not display "a"?
The verbose equivalent of print(myVar[::-1]) would be:
print(myVar[-1:-1-len(myVar):-1])
# Or
# print(myVar[len(myVar)-1:-1-len(myVar):-1])
# but this makes the the length invariant less obvious
Note that the stop parameter is exclusive, and in order to get to the actual -1, you have to additionally subtract the full length as negative indexing starts at the end of the sequence. Note also how (stop-start)*step is still the length of the slice.
This question already has answers here:
creating sum of odd indexes python
(3 answers)
Closed 6 years ago.
Say I call this function getSumOdds([1,2,3,4,5]). This should return 6 since 2 + 4 are located at odd indices. I have figured out how to get it to return what numbers are located at odd indices, but I am unsure how to add them.
getSumOdds(aList):
return aList[1::2]
All this does is return what numbers are at the odd locations. I also want it just to return aList. I have tried using sum() in various ways but nothing seems to work. Anything would help!!
getSumOdds(aList):
return sum(aList[1::2])
Are you looking for sum(aList[1::2]) ?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I tried searching the python documentation for a certain slicing exapmle
lets say I have this string
a = "ABCD"
when I write:
a[::-1]
I get the reversed string
"DCBA"
I can't understand how exectaly it works. None of the examples I saw were with two colons. what does it mean? how does it work?
thank you!
The full syntax of a slicing is
a[start:stop:step]
The step value denotes by how much to increase the index when going from one element of the slice to the next one. A value of -1 consequently means "go backwards". If start and stop are omitted as in your example, the default is to use the whole string (or more generally, the whole sequence, as this also works for lists and tuples).