The problem
You're appending the same array a to your list0 4 times. Arrays like a are mutable objects, which means, among other things, that when you assign values to them the underlying object changes. Since the array is present in your list 4 times, those changes (seem to) show up in 4 different places.
Solution
You can fix the code you have with one small change. Append a copy of the array to your list, instead of the array itself:
import numpy as np
a = np.empty((3), int)
list0 = []
for idx in range(4):
for i in range(3):
a[i] = idx*10 + i
print("idx =",idx,"; a =",a)
list0.append(a.copy())
print("list0 =",list0)
Output:
idx = 0 ; a = [0 1 2]
idx = 1 ; a = [10 11 12]
idx = 2 ; a = [20 21 22]
idx = 3 ; a = [30 31 32]
list0 = [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
Optimized solution
Python/Numpy offer many better ways (both in terms of using fewer lines of code and running faster) to initialize arrays. For a bunch of ranges like this, here is a reasonable approach:
list0 = [np.arange(n*10, n*10+3) for n in range(4)]
print(list0)
Output:
[array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
You might also consider just using a single 2D array in place of a list of arrays. One single array is often easier to work with than a heterogenous mix of arrays in a list. Here's how you do that:
arr0 = np.array([np.arange(n*10, n*10+3) for n in range(4)])
print(arr0)
Output:
[[ 0 1 2]
[10 11 12]
[20 21 22]
[30 31 32]]
Answer from tel on Stack OverflowThe problem
You're appending the same array a to your list0 4 times. Arrays like a are mutable objects, which means, among other things, that when you assign values to them the underlying object changes. Since the array is present in your list 4 times, those changes (seem to) show up in 4 different places.
Solution
You can fix the code you have with one small change. Append a copy of the array to your list, instead of the array itself:
import numpy as np
a = np.empty((3), int)
list0 = []
for idx in range(4):
for i in range(3):
a[i] = idx*10 + i
print("idx =",idx,"; a =",a)
list0.append(a.copy())
print("list0 =",list0)
Output:
idx = 0 ; a = [0 1 2]
idx = 1 ; a = [10 11 12]
idx = 2 ; a = [20 21 22]
idx = 3 ; a = [30 31 32]
list0 = [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
Optimized solution
Python/Numpy offer many better ways (both in terms of using fewer lines of code and running faster) to initialize arrays. For a bunch of ranges like this, here is a reasonable approach:
list0 = [np.arange(n*10, n*10+3) for n in range(4)]
print(list0)
Output:
[array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]
You might also consider just using a single 2D array in place of a list of arrays. One single array is often easier to work with than a heterogenous mix of arrays in a list. Here's how you do that:
arr0 = np.array([np.arange(n*10, n*10+3) for n in range(4)])
print(arr0)
Output:
[[ 0 1 2]
[10 11 12]
[20 21 22]
[30 31 32]]
Just do this:
list_to_append.append(np_array.copy())
In a nutshell, numpy arrays or lists are mutable objects, which means that you when you assign a numpy array or list to a variable, what you are really assigning are references to memory locations aka pointers.
In your case, "a" is a pointer, so what you are really doing is appending to list0 an address to the memory location pointed by "a", and not the actual values pointed by the pointer. Thus it means that each new position of "list0", after appending, turns out to be the same address of memory: "a".
So, instead of:
list0.append(a)
You call the copy() method of "a" that creates a new memory location for the new values of "a" and returns it:
list0.append(a.copy())
Videos
You can append the elements of one list to another with the "+=" operator. Note that the "+" operator creates a new list.
a = [1, 2, 3]
b = [10, 20]
a = a + b # Create a new list a+b and assign back to a.
print a
# [1, 2, 3, 10, 20]
# Equivalently:
a = [1, 2, 3]
b = [10, 20]
a += b
print a
# [1, 2, 3, 10, 20]
If you want to append the lists and keep them as lists, then try:
result = []
result.append(a)
result.append(b)
print result
# [[1, 2, 3], [10, 20]]
Apart from + operator there's another way to do the same i.e. extend()
a = [1, 2, 3]
b = [10, 20]
a.append(b) # Output: [1, 2, 3, [10, 20]]
a.extend(b) # Output: [1, 2, 3, 10, 20]
You can use these 2 functions for manipulating a list as per your requirement.
say i have array [[1,2,3,4,5]], how can i duplicate it 5 times to [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] ?
{} represents an empty dictionary, not an array/list. For lists or arrays, you need [].
To initialize an empty list do this:
my_list = []
or
my_list = list()
To add elements to the list, use append
my_list.append(12)
To extend the list to include the elements from another list use extend
my_list.extend([1,2,3,4])
my_list
--> [12,1,2,3,4]
To remove an element from a list use remove
my_list.remove(2)
Dictionaries represent a collection of key/value pairs also known as an associative array or a map.
To initialize an empty dictionary use {} or dict()
Dictionaries have keys and values
my_dict = {'key':'value', 'another_key' : 0}
To extend a dictionary with the contents of another dictionary you may use the update method
my_dict.update({'third_key' : 1})
To remove a value from a dictionary
del my_dict['key']
If you do it this way:
array = {}
you are making a dictionary, not an array.
If you need an array (which is called a list in python ) you declare it like this:
array = []
Then you can add items like this:
array.append('a')