You haven't created three different empty lists. You've created one empty list, and then created a new list with three references to that same empty list. To fix the problem use this code instead:
listy = [[] for i in range(3)]
Running your example code now gives the result you probably expected:
>>> listy = [[] for i in range(3)]
>>> listy[1] = [1,2]
>>> listy
[[], [1, 2], []]
>>> listy[1].append(3)
>>> listy
[[], [1, 2, 3], []]
>>> listy[2].append(1)
>>> listy
[[], [1, 2, 3], [1]]
Answer from Mark Byers on Stack OverflowAppending to 2D lists in Python - Stack Overflow
Two dimensional array in python - Stack Overflow
do you guys know how to add append an array in a 2d array?
arrays - Python: multi-dimensional lists - appending one 2D list into another list - Stack Overflow
You haven't created three different empty lists. You've created one empty list, and then created a new list with three references to that same empty list. To fix the problem use this code instead:
listy = [[] for i in range(3)]
Running your example code now gives the result you probably expected:
>>> listy = [[] for i in range(3)]
>>> listy[1] = [1,2]
>>> listy
[[], [1, 2], []]
>>> listy[1].append(3)
>>> listy
[[], [1, 2, 3], []]
>>> listy[2].append(1)
>>> listy
[[], [1, 2, 3], [1]]
[[]]*3 is not the same as [[], [], []].
It's as if you'd said
a = []
listy = [a, a, a]
In other words, all three list references refer to the same list instance.
You do not "declare" arrays or anything else in python. You simply assign to a (new) variable. If you want a multidimensional array, simply add a new array as an array element.
arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')
or
arr = []
arr.append(['aa1', 'aa2'])
There aren't multidimensional arrays as such in Python, what you have is a list containing other lists.
>>> arr = [[]]
>>> len(arr)
1
What you have done is declare a list containing a single list. So arr[0] contains a list but arr[1] is not defined.
You can define a list containing two lists as follows:
arr = [[],[]]
Or to define a longer list you could use:
>>> arr = [[] for _ in range(5)]
>>> arr
[[], [], [], [], []]
What you shouldn't do is this:
arr = [[]] * 3
As this puts the same list in all three places in the container list:
>>> arr[0].append('test')
>>> arr
[['test'], ['test'], ['test']]
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]] ?
Simple, use + operator. You could concatenate two lists using + operator.
>>> list1 = [('a', '1'), ('b', '2'), ('c', '3')]
>>> list2 = [('d', '4'), ('e', '5'), ('f', '6')]
>>> list2 + list1
[('d', '4'), ('e', '5'), ('f', '6'), ('a', '1'), ('b', '2'), ('c', '3')]
You can use extend to modify list2 inplace:
>>> list2.extend(list1)
>>> list2
[('d', '4'), ('e', '5'), ('f', '6'), ('a', '1'), ('b', '2'), ('c', '3')]
This is particularly efficient if you're extending a large list, since no copy of that list is made: the operation is O(k) in complexity, where k is the length of the list you're adding on.
This is essentially the same behaviour as the code in your question that, as others have pointed out, should work. In practice extend should be a little faster because the loop over list1 is moved down to C level.
ValueError: too many values to unpack indicates that one or more of the tuples in your list final_list has more than two elements. This causes the line for x, y in final_list: to raise the error since x and y can't label every element in the tuple.
You can just extend your list.
data = []
data.extend(d1)
data.extend(d2)
data.extend(d3)
Simply write:
all_data = data_1 + data_2 + data_3
If you want to merge all the corresponding sublists of these lists, you can write:
# function that merges all sublists in single list
def flatten(l):
return [el for subl in l for el in subl]
# zipped - is a list of tuples of corresponding items (our deep sublists) of all the lists
zipped = zip(data_1, data_3, data_3)
# merge tuples to get single list of rearranges sublists
all_data = flatten(zipped)