I tried to work with a list of deques and was not able to to append a value to a single deque by indexing it. It looks like the following: &gt;&gt;&gt; l_deque = [deque([0] * 3, maxlen = 3)] * 5 &gt;&gt;&gt; l_deque [deque([0, 0, 0]),  deque([0, 0, 0]),  deque([0, 0, 0]),  deque([0, 0, 0]),  deque([0, 0, 0])]  &gt;&gt;&gt; l_deque[0] deque([0, 0, 0])  So far so good. But when I try to append a value to a single deque it appends it to every deque: &gt;&gt;&gt; l_deque[0].append(1) &gt;&gt;&gt; l_deque [deque([0, 0, 1]),  deque([0, 0, 1]),  deque([0, 0, 1]),  deque([0, 0, 1]),  deque([0, 0, 1])]  I expected something like this: [deque([0, 0, 1]),  deque([0, 0, 0]),  deque([0, 0, 0]),  deque([0, 0, 0]),  deque([0, 0, 0])]  Do you have an idea where I took the wrong turn? Thanks Edit: Thanks guys, Changing it to     &gt;&gt;&gt; l = [deque([0] * 3, maxlen = 3),              deque([0] * 3, maxlen = 3),              deque([0] * 3, maxlen = 3),              deque([0] * 3, maxlen = 3),              deque([0] * 3, maxlen = 3)]  did the job. Ugly but effective.
410
I needed to create a list of lists in Python, so I typed the following: my_list = [[1] * 4] * 3  The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]    Then I changed one of the innermost values: my_list[0][0] = 5  Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]    which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?
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]
106324
I want to write a function that accepts a parameter which can be either a sequence or a single value. The type of value is str, int, etc., but I don't want it to be restricted to a hardcoded list.  In other words, I want to know if the parameter X is a sequence or something I have to convert to a sequence to avoid special-casing later. I could do  type(X) in (list, tuple)  but there may be other sequence types I'm not aware of, and no common base class.  -N.  Edit: See my "answer" below for why most of these answers don't help me. Maybe you have something better to suggest.
98693
You can turn any element $n$ into a singleton set by adding braces, $\{n\}$. Is there an inverse to this operation, such that if I know the set is a singleton set I can easily reference its element?  For example, say I have the set $A = \{5,6,7,8\}$, and then some process that iteratively eliminates all but one element of $A$, and then I want to see what 10 plus the resulting element is, how could I write that?  The only way I can think of is by referencing the first element by its index, e.g., $10 + A_1$. Is there another way to do it though? In my case the elements of $A$ are already indices, so I would have to index an index, like $10 + x_{A_1}$. Which is not the worst thing in the world, I was just wondering if there is a better way.
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
7849
See the following MWE to understand what I mean better.  \documentclass[preview,border=12pt]{standalone} \usepackage{pgffor,multido} \begin{document} \section*{foreach} \foreach \ix in {0,1,...,3}{%     \foreach \iy in {0,1,...,\ix}{(\ix,\iy)\ }\endgraf}  \section*{multido} \multido{\ix=0+1}{4}{%     \multido{\iy=0+1}{\the\numexpr\ix+1\relax}{(\ix,\iy)\ }\endgraf} \end{document}     Why cannot \foreach \x in {0,1,...,0}{} discharge the second iteration?
8317
If I have sequence of sequences (maybe a list of tuples) I can use itertools.chain() to flatten it. But sometimes I feel like I would rather write it as a comprehension. I just can't figure out how to do it. Here's a very construed case:  Let's say I want to swap the elements of every pair in a sequence. I use a string as a sequence here:  &gt;&gt;&gt; from itertools import chain &gt;&gt;&gt; seq = '012345' &gt;&gt;&gt; swapped_pairs = zip(seq[1::2], seq[::2]) &gt;&gt;&gt; swapped_pairs [('1', '0'), ('3', '2'), ('5', '4')] &gt;&gt;&gt; "".join(chain(*swapped_pairs)) '103254'   I use zip on the even and odd slices of the sequence to swap the pairs. But I end up with a list of tuples that now need to be flattened. So I use chain(). Is there a way I could express it with a comprehension instead?  If you want to post your own solution to the basic problem of swapping elements of the pairs, go ahead, I'll up-vote anything that teaches me something new. But I will only mark as accepted an answer that is targeted on my question, even if the answer is "No, you can't.".
112773
I need to be able to take a string like:  '''foo, bar, "one, two", three four'''   into:  ['foo', 'bar', 'one, two', 'three four']   I have an feeling (with hints from #python) that the solution is going to involve the shlex module.
57724
Is it possible to stack three relations or symbols ontop of one another? Specifically I would like to stack three symbols, R, \rightleftarrows and L in order to define a pair of adjoint functors $R:C\to D$ and $C\gets D:L$.  Also, is there a \rightleftarrow with long arrows? EDIT I actually meant to ask wether there is a version of \rightleftarrows with long arrows.  I'm not a regular user, so I don't know how to format my question adequately, could someone convert what's inside the dollar so that it appears as latex please?
37834
Sometimes, I want to have enumerate lists in LaTeX start at other than the first value (1, a, i, etc.) How can I make an enumerate list start at an arbitrary value?
28318
I'm trying to format numbers. Examples:  1     =&gt; 1 12    =&gt; 12 123   =&gt; 123 1234  =&gt; 1,234 12345 =&gt; 12,345   It strikes as a fairly common thing to do but I can't figure out which filter I'm supposed to use.  Edit: If you've a generic Python way to do this, I'm happy adding a formatted field in my model.
94174
When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad.  For example, you can't do this:  for name in names:     if name[-5:] == 'Smith':         names.remove(name)   I usually end up doing something like this:  toremove = [] for name in names:     if name[-5:] == 'Smith':         toremove.append(name) for name in toremove:     names.remove(name) del toremove   This is innefficient, fairly ugly and possibly buggy (how does it handle multiple 'John Smith' entries?). Does anyone have a more elegant solution, or at least a more efficient one?   How about one that works with dictionaries?
90370
What is wrong with this command? It says it has a problem with value separators, but i can't find it  /summon ArmorStand ~ ~ ~10 {ShowArms:1,NoBasePlate:1,Pose:{LeftArm:[0f,0f0f],RightArm:[0f,0f,0f],Body:[0f,0f,0f],LeftLeg:[0f,0f,0f],RightLeg:[0f,0f,0f],Head:[0f,0f,0f]},ArmorItems:{id:diamond_boots,ench:[{id:protection,lvl:1000},{id:blast_protection,lvl:1000},{id:thorns,lvl:1000},{id:unbreaking,lvl:1000}]},{id:diamond_leggings,ench:[{id:protection,lvl:1000},{id:blast_protection,lvl:1000}, {id:thorns,lvl:1000},{id:unbreaking,lvl:1000}]}, {id:diamond_chestplate,ench:[{id:protection,lvl:1000},{id:blast_protection,lvl:1000},{id:thorns,lvl:1000},{id:unbreaking,lvl:1000}]},{id:diamond_helmet,ench:[{id:protection,lvl:1000},{id:blast_protection,lvl:1000},{id:thorns,lvl:1000},{id:unbreaking,lvl:1000}]}}
10538
What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ['a', 'b', 'c'] to 'a,b,c'? (The cases ['s'] and [] should be mapped to 's' and '', respectively.)  I usually end up using something like ''.join(map(lambda x: x+',',l))[:-1], but also feeling somewhat unsatisfied.
44995
I had this question earlier and it was concluded it was a bug in 5.2.5.  Well, it's still broken in 5.2.6, at least for me:  Please let me know if it is broken or works for you:  $obj = new stdClass(); $obj-&gt;{"foo"} = "bar"; $obj-&gt;{"0"} = "zero"; $arr = (array)$obj;  //foo -- bar //0 --    {error: undefined index} foreach ($arr as $key=&gt;$value){     echo "$key -- " . $arr[$key] . "\n"; }   Any ways to "fix" the array after it has been cast from a stdClass?
19558
How many injective functions  $f:[1,...,m]\to{[1,...,n]}$ has no fixed point? $(m\le n)$  I thought about the next thing:  $f(x_1)\neq x_1$, Means i can choose for $x_1$ - (n-1) options,  But then, for $x_2$, there are two options:   If i choose $f(x_1)=x_2$ then for $x_2$ i still have (n-1) options. If i choose $f(x_1)\ne x_2$ then for $x_2$ i'll have (n-2) options.   So how can i take care of that? Or am i looking the question totally wrong?
34678
I've a List of this type List> that contains this  List&lt;int&gt; A = new List&lt;int&gt; {1, 2, 3, 4, 5}; List&lt;int&gt; B = new List&lt;int&gt; {0, 1}; List&lt;int&gt; C = new List&lt;int&gt; {6}; List&lt;int&gt; X = new List&lt;int&gt; {....,....};   I want to have all combinations like this  1-0-6 1-1-6 2-0-6 2-1-6 3-0-6   and so on.  According to you is This possibile to resolve using Linq?