Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python appending array to an array

Tags:

python

arrays

I am currently working on DES implementation.In one part of the code I have to append array to an array.Below is my code:

C0=[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]  def Fiestel():     C=[]     C.append(C0)     temp=[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1]     C.append(temp)     print(C) Fiestel() 

How do I append an array to an existing array.I even tried declaring C as 2d array.Thankx in advance for helping.

Each element is an array itself.

enter image description here

like image 598
shubhamj Avatar asked Oct 31 '16 04:10

shubhamj


People also ask

How do you append an array to an array?

To append one array to another, use the push() method on the first array, passing it the values of the second array. The push method is used to add one or more elements to the end of an array. The method changes the contents of the original array. Copied!

How do you append two arrays in Python?

How to concatenate NumPy arrays in Python? You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.

Can you use .append on an array?

We can create an array using the Array module and then apply the append() function to add elements to it.

How do you initialize and append an array in Python?

you need to do: array = array[] in order to define it, and then: array. append ["hello"] to add to it.


1 Answers

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]] 
like image 101
stackoverflowuser2010 Avatar answered Oct 05 '22 23:10

stackoverflowuser2010