I am trying to change an element in a Python list. Following the tutorial on , I came up with the code below.   matrix = [[0]*6]*3  print(matrix)  matrix[0][0] = 2  print(matrix)   Upon running the code, I received the following output:  [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] [[2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0]]   Notice the last line of output, how the first element of every sub-list is set to 2. How do I make it so that only the first element of the first list is changed.
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?
53456
What is the difference between:  some_list1 = [] some_list1.append("something")   and  some_list2 = [] some_list2 += ["something"]
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
32263
Can you please tell me how much is (-2) % 5? According to my Python interpreter is 3, but do you have a wise explanation for this?  I've read that in some languages the result can be machine-dependent, but I'm not sure though.
36364
Is there a way to access a list's (or tuple's, or other iterable's) next or previous element while looping through it with a for loop?  l = [1, 2, 3] for item in l:     if item == 2:         get_previous(l, item)
114975
I need a list of integers from 1 to x where x is set by the user.  I could build it with a for loop eg assuming x is an integer set previously:  List&lt;int&gt; iList = new List&lt;int&gt;(); for (int i = 1; i &lt;= x; i++) {     iList.Add(i); }   This seems dumb, surely there's a more elegant way to do this, something like the
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?
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?
9685
I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1 &amp; 20), I have written for the program to display all Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 displays = First 20 Fibonacci numbers). I thought I had a sure-fire code. I also do not see why this is happening.  startNumber = int(raw_input("Enter the start number here ")) endNumber = int(raw_input("Enter the end number here "))  def fib(n):     if n &lt; 2:         return n     return fib(n-2) + fib(n-1)  print map(fib, range(startNumber, endNumber))   Someone pointed out in my Part II (which was closed for being a duplicate - ) that I need to pass the startNumber and endNumber through a generator using a while loop. Can someone please point me in the direction on how to do this? Any help is welcome.    I'm a learning programmer and I've run into a bit of a jumble. I am asked to write a program that will compute and display Fibonacci's Sequence by a user inputted start number and end number (ie. startNumber = 20 endNumber = 100 and it will display only the numbers between that range). The trick is to use it inclusively (which I do not know how to do in Python? - I'm assuming this means to use an inclusive range?).  What I have so far is no actual coding but rather:   Write Fib sequence formula to infinite Display startNumber to endNumber only from Fib sequence.   I have no idea where to start and I am asking for ideas or insight into how to write this. I also have tried to write the Fib sequence forumla but I get lost on that as well.
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).
74382
i have:  for i in range(2,n):     if(something):        do something     else:        do something else        i = 2 **restart the loop   But that doesn't seem to work. Is there a way to restart that loop?   Thanks
76152
int t=-1'd1; //trying to assign -1 to t; bit vb[7:0]; // simple declaration  bit matrix[0:1][0:7]='{'{0,1,0,0,0,0,0,0},'{0,0,1,0,0,0,0,0},'{1,0,0,1,0,0,0,0},'{0,0,0,0,1,0,0,0},'{0,0,0,0,0,1,0,1},'{0,0,0,0,0,0,1,0},'{0,0,0,0,1,0,0,1},'{0,0,0,0,0,0,0,0}};// initialising multi-dimensional array.  I am using QuestaSim to compile and simulate. this is the report: # -- Compiling module test # ** Error: (vlog-13032) test.v(8): near &quot;=&quot;: Syntax error. # ** Error: (vlog-13032) test.v(9): near &quot;;&quot;: Syntax error.  I am somewhat New to the Verilog. Please help me in fixing these errors.
1457
I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item.  For example:  original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)] # and I want to become... result = (['a', 'b', 'c', 'd'], [1, 2, 3, 4])   Is there a builtin function that does that?
28573
I want to make an enumerated list in the following format:  [1] foo foo [2] foo foo ...    How I can do that using enumerated lists? I have tried the following:  \begin{enumerate}[\left[ 1 \right]]   but it does not work.
101984
I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?  Example:    testlist = [1,2,3,5,3,1,2,1,6] for item in testlist:     if item == 1:         print position
18119
I know there is a method for a Python list to return the first index of something:  &gt;&gt;&gt; l = [1, 2, 3] &gt;&gt;&gt; l.index(2) 1   Is there something like that for NumPy arrays?
2695
Some of you may recognize this as Project Euler's problem number 11. The one with the grid.  I'm trying to replicate the grid in a large multidimensional array, But it's giving me a syntax error and i'm not sure why  grid = [  [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 ], [ 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 ], [ 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65 ], ...   And I get this error:     File "D:\development\Python\ProjectEuler\p11.py", line 3     [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91 , 08 ],        ^ SyntaxError: invalid token   Why is it throwing an error before the comma?