Why does this not print the last element in the list.  &gt;&gt;&gt; a_lst = [1,2,3,4,5,6] &gt;&gt;&gt; print a_lst[-1] 6 &gt;&gt;&gt; print a_lst[0:-1] [1, 2, 3, 4, 5]   I can see that if I just do a_lst[-1] it returns the expected last element.  But when I try to use it in a range [0:-1] it actually returns the penultimate element?
106
I need a good explanation (references are a plus) on Python's slice notation.   To me, this notation needs a bit of picking up.   It looks extremely powerful, but I haven't quite got my head around it.
467
How do you generate all the permutations of a list in Python, independently of the type of elements in that list?  For example:  permutations([]) []  permutations([1]) [1]  permutations([1, 2]) [1, 2] [2, 1]  permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
106641
How do I format a list of questions like this?     I'm getting the equation on a new line below the i) when using enumerate and \list.  I don't want to get rid of \[ and \] either as I like the long integral sign  I get this instead:
44451
The table looks like this:  1|Hunk|First|London 2|Hero|Second|London 3|Will|First|Pow 4|Thomas|Second|Pow 5|Venus|First|Berlin   I want to print the output as:  Berlin|First|venus|5   i want to print only the final line.
111922
I came across the following problem and do not know how to tackle it.       Find the last two digits of $ 7^{81} ?$     Can someone point me in the right direction? Thanks in advance for your time.
106392
To list all the files ending with 10 or 11 or 12 I have tried ls *[10-12] and ls *[10,11,12] but these are not working. I don't know why. Can anyone help me?
12659
In many symbolic math systems, such as Matlab or Mathematica, you can use a variable like Ans or % to retrieve the last computed value. Is there a similar facility in the Python shell?
25863
Is there a library function that performs binary search on a list/tuple and return the position of the item if found and 'False' (-1, None, etc.) if not?  I found the functions bisect_left/right in the , but they still return a position even if the item is not in the list. That's perfectly fine for their intended usage, but I just want to know if an item is in the list or not (don't want to insert anything).  I thought of using bisect_left and then checking if the item at that position is equal to what I'm searching, but that seems cumbersome (and I also need to do bounds checking if the number can be larger than the largest number in my list). If there is a nicer method I'd like to know about it.  Edit To clarify what I need this for: I'm aware that a dictionary would be very well suited for this, but I'm trying to keep the memory consumption as low as possible. My intended usage would be a sort of double-way look-up table. I have in the table a list of values and I need to be able to access the values based on their index. And also I want to be able to find the index of a particular value or None if the value is not in the list.  Using a dictionary for this would be the fastest way, but would (approximately) double the memory requirements.  I was asking this question thinking that I may have overlooked something in the Python libraries. It seems I'll have to write my own code, as Moe suggested.
29934
I'm sure there's a nice way to do this in Python, but I'm pretty new to the language, so forgive me if this is an easy one!  I have a list, and I'd like to pick out certain values from that list. The values I want to pick out are the ones whose indexes in the list are specified in another list.  For example:  indexes = [2, 4, 5] main_list = [0, 1, 9, 3, 2, 6, 1, 9, 8]   the output would be:  [9, 2, 6]   (i.e., the elements with indexes 2, 4 and 5 from main_list).  I have a feeling this should be doable using something like list comprehensions, but I can't figure it out (in particular, I can't figure out how to access the index of an item when using a list comprehension).
7042
So I can start from len(collection) and end in collection[0].  I also want to be able to access the loop index.
35871
How does Python evaluate the expression 1+++2?  How many ever + I put in between, it is printing 3 as the answer. Please can anyone explain this behavior  And for 1--2 it is printing 3 and for 1---2 it is printing -1
90301
Formatting breaks if code follows a list:   List item List item  This is in a code block  This does not work as expected   However, it works if between the list and the code   List item List item   There is a line of normal text.  This is in a code block # This does work as expected   Edit  To make this clear: This is not about nesting code in a list but about code following a (finished) list not working as expected (by me). To put it differently: Why is a list not considered finished despite of following empty lines? What do I have to do?
72908
I am trying to figure out this:  c = 1 def f(n):     print c + n  def g(n):     c = c + n  f(1) # =&gt; 2 g(1) # =&gt; UnboundLocalError: local variable 'c' referenced before assignment   Thanks!
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
49176
Why does python 2.5.2 have the following behavior  &gt;&gt;&gt;[2].extend([]) == [2] False  &gt;&gt;&gt; [2].extend([]) == None True  $ python --version Python 2.5.2   I assume I'm not understanding something here, but intuitively I'd think that [2].extend([]) should yield [2]
34147
I'm trying to identify null/0 values in each of my raster bands. How can I loop through all the values of each band? I've tried this code, straight from the gdal/python cookbook website but this just gives stats for each band as a whole and for some reason it actually doesn't print a minimum value of 0 even though my raster does have 0 values, it shows a minimum value of 1.   I want to print every single cell value. Is there a way to do this?   from osgeo import gdal import sys  src_ds = gdal.Open( "INPUT.tif" )  print "[ RASTER BAND COUNT ]: ", src_ds.RasterCount for band in range( src_ds.RasterCount ):     band += 1     print "[ GETTING BAND ]: ", band     srcband = src_ds.GetRasterBand(band)      stats = srcband.GetStatistics( True, True )      print "[ STATS ] =  Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % ( \             stats[0], stats[1], stats[2], stats[3] )
44787
If you are creating a 1d array, you can implement it as a List, or else use the 'array' module in the standard library.  I have always used Lists for 1d arrays.  What is the reason or circumstance where I would want to use the array module instead?  Is it for performance and memory optimization, or am I missing something obvious?