init = [[0,1],[10,11]] constr1 = [init for _ in range(5)] constr2 = init * 5   First question, why the above 2 constructed lists do not give the same result and they are instead   constr1  [[[0, 1], [10, 11]], [[0, 1], [10, 11]], [[0, 1], [10, 11]], [[0, 1], [10, 11]], [[0, 1], [10, 11]]]  constr2 [[0, 1], [10, 11], [0, 1], [10, 11], [0, 1], [10, 11], [0, 1], [10, 11], [0, 1], [10, 11]]   And the other question is if I edit the initial list  init[0][1]=2   then we can see that both constr1 and constr2 have been affected and they are   [[[0, 2], [10, 11]], [[0, 2], [10, 11]], [[0, 2], [10, 11]], [[0, 2], [10, 11]], [[0, 2], [10, 11]]]   and  [[0, 2], [10, 11], [0, 2], [10, 11], [0, 2], [10, 11], [0, 2], [10, 11], [0, 2], [10, 11]]   So what is the way to get a constructed list (by repetition of list init) and the constr list NOT being affected by any subsequent changes in the init list? NOTE I also tried the line (after clearing all my variables by restarting my python shell)   constr1 = [init[:] for _ in range(5)]   and still it is affected by a change in init[0][1]
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?
2267
What's the difference between the list methods append() and extend()?
10523
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
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?
16381
I would want to do something like:  &gt;&gt;&gt; lst = [1, 2, 3, 4, 5] &gt;&gt;&gt; lst.find(lambda x: x % 2 == 0) 2 &gt;&gt;&gt; lst.findall(lambda x: x % 2 == 0) [2, 4]   Is there anything nearing such behavior in Python's standard libraries?  I know it's very easy to roll-your-own here, but I'm looking for a more standard way.
18652
I have the following class in C++:  class a {     const int b[2];     // other stuff follows      // and here's the constructor     a(void); }   The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const?  This doesn't work:  a::a(void) :      b([2,3]) {      // other initialization stuff }   Edit:  The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance.
16494
When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
78279
I'm sure there's a simpler way of doing this that's just not occurring to me.  I'm calling a bunch of methods that return a list.  The list may be empty.  If the list is non-empty, I want to return the first item; otherwise, I want to return None.  This code works:  my_list = get_list() if len(my_list) &gt; 0: return my_list[0] return None   It seems to me that there should be a simple one-line idiom for doing this, but for the life of me I can't think of it.  Is there?  Edit:  The reason that I'm looking for a one-line expression here is not that I like incredibly terse code, but because I'm having to write a lot of code like this:  x = get_first_list() if x:     # do something with x[0]     # inevitably forget the [0] part, and have a bug to fix y = get_second_list() if y:     # do something with y[0]     # inevitably forget the [0] part AGAIN, and have another bug to fix   What I'd like to be doing can certainly be accomplished with a function (and probably will be):  def first_item(list_or_none):     if list_or_none: return list_or_none[0]  x = first_item(get_first_list()) if x:     # do something with x y = first_item(get_second_list()) if y:     # do something with y   I posted the question because I'm frequently surprised by what simple expressions in Python can do, and I thought that writing a function was a silly thing to do if there was a simple expression could do the trick.  But seeing these answers, it seems like a function is the simple solution.
105396
What is the difference between  char str1[32] = "\0";   and   char str2[32] = "";
1520
I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] and makes it into iterable list of tuples, like so: [(0,3), (1,7), (2,19)] to use it instead of:  for i in range(len(name_of_list)):     name_of_list[i] = something   but I can't remember the name and googling "iterate list" gets nothing.
119860
A weird bug was occurring in production which I was asked to look into. The issue was tracked down to a couple of variables being declared within a For loop and not being initialized on each iteration. An assumption had been made that due to the scope of their declaration they would be &quot;reset&quot; on each iteration. Could someone explain why they would not be)? (My first question, really looking forward to the responses.) The example below is obviously not the code in question but reflects the scenario:  Please excuse the code example, it looks fine in the editor preview?? for (int i =0; i&lt; 10; i++) {     decimal? testDecimal;     string testString;      switch( i % 2  )     {         case 0:         testDecimal = i / ( decimal ).32;         testString = i.ToString();             break;         default:             testDecimal = null;             testString = null;             break;     }      Console.WriteLine( &quot;Loop {0}: testDecimal={1} - testString={2}&quot;, i, testDecimal , testString ); }   EDIT: Sorry, had to rush out for child care issue. The issue was that the prod code had was that the switch statement was huge and in some &quot;case&quot;'s a check on a class' property was being made, like if (myObject.Prop != null) then testString = myObject.Stringval... At the end of the switch, (outside) a check on testString == null was being made but it was holding the value from the last iteration,hence not being null as the coder assumed with the variable being declared within the loop. Sorry if my question and example was a bit off, I got the phone call about the day care as I was banging it together. I should have mentioned I compared IL from both variables in and out the loop. So, is the common opinion that &quot;obviously the variables would not be reinitialized on each loop&quot;? A little more info, the variables WHERE being initialized on each iteration until someone got over enthusiastic with ReSharper pointing out &quot;the value is never used&quot; and removed them.  EDIT: Folks, I thank you all. As my first post I see how much clearer I should be in the future. The cause of our unexpected variable assignment can me placed on an inexperienced developer doing everything ReSharper told him and not running any unit tests after he ran a &quot;Code Cleanup&quot; on an entire solution. Looking at the history of this module in VSS I see variables Where declared outside of the loop and where initialized on each iteration. The person in question wanted his ReSharper to show &quot;all green&quot; so &quot;moved his variables closer to assignment&quot; then &quot;Removed redundant assignment&quot;! I don't think he will be doing it again...now to spend the weekend running all the unit tests he missed! How to do mark a question as answered?
8585
Is there a built-in that removes duplicates from list in Python, whilst preserving order? I know that I can use a set to remove duplicates, but that destroys the original order. I also know that I can roll my own like this:  def uniq(input):   output = []   for x in input:     if x not in output:       output.append(x)   return output   (Thanks to  for that .)  But I'd like to avail myself of a built-in or a more Pythonic idiom if possible.  Related question:
89893
The __debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it?  The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense that if I change foo in one module, it is updated in others. I'd be fine if I could set foo before importing other modules and then they would see the same value for it.
6852
What is the difference between below variables assignments?   var=23 var =23 var= 23 var = 23   Is there any difference in space around the assignment operator?
106578
When editing , I noticed that placing breaks (blank lines) in lists (especially nested lists) is inconsistent (and possibly dependent on the current state of the JavaScript variables of the editor).  Different layouts can give the same results, multiple changes, changes to the wrong part, or no changes at all.  For example, with the following list, there are no line-breaks before or after anything as expected:    1. foo     * bar     * baz   2. test     * me     * you   Gives this:   foo   bar baz  test   me you    If you try to add a space between the two top-level lists (before the line with 2. test), you would naturally use this:    1. foo     * bar     * baz    2. test     * me     * you   Except that you get this unexpected and unwanted result instead:   foo   bar baz  test   me you    In this state, adding or removing a blank line between the first- and second-level lists (after foo or test) changes nothing:    1. foo      * bar     * baz    2. test     * me     * you   Is the same as before:   foo   bar baz  test   me you    Look at the marked rows in the following screenshot; different layouts give the same results (which often don’t match what was expected):    A video is worth a million words (, ). The closest related question I could find was .    (“By design?” So making it work inconsistently and give unexpected and unwanted results is on purpose‽ ಠ_ఠ)
33748
What is the difference between int *a[3] and int (*a)[3]?
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!