61

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

Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
shubhamj
  • 778
  • 1
  • 7
  • 13
  • 4
    Check the `extend` method – Francisco Oct 31 '16 at 04:31
  • 1
    I want the next element to be an array. I tried that but still no correct answer – shubhamj Oct 31 '16 at 04:36
  • 1
    Your code creates a list `C` that contains two lists. What is the problem? – TigerhawkT3 Oct 31 '16 at 04:39
  • Your question is not clear, so anyone who attempts to answer it is making assumptions about what you want. You haven't really said what is wrong and also you haven't provided the desired output. Either of those should make your desired outcome clearer. – Paul Rooney Oct 31 '16 at 04:48
  • Sorry for not being able to explain properly.I just uploaded the image explaining the output I wanted.I got the output somehow – shubhamj Oct 31 '16 at 04:51
  • You appear to already have the output you desire. What is this question about? – TigerhawkT3 Oct 31 '16 at 05:20
  • Could the people who upvoted this explain what this question is actually asking? To me, it looks like "I'm trying A in order to get X, but I'm getting X. How do I get X?" And the answers are either "do A to get X" or "do B to get Y." – TigerhawkT3 Oct 31 '16 at 05:59

4 Answers4

116

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]]
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • 1
    Instead of that I want new array to be [[1,2,3],[10,20]] – shubhamj Oct 31 '16 at 04:40
  • 1
    @shubhamj: Added more code. You should have specified what your desired output is. – stackoverflowuser2010 Oct 31 '16 at 04:44
  • OP is already doing this. It isn't clear what exactly is going wrong. This answer adds nothing. – TigerhawkT3 Oct 31 '16 at 05:19
  • 4
    @TigerhawkT3: The question has been edited six times. What you see now is not what was written initially. – stackoverflowuser2010 Oct 31 '16 at 05:49
  • And what they wrote initially would do exactly what they wanted (assuming correct indentation): create a list and append two other lists to it. You are telling them to do something they are already doing. What is the purpose of this answer? You even left the incorrect suggestion in there at the top. I don't know why this is upvoted. – TigerhawkT3 Oct 31 '16 at 05:58
  • @TigerhawkT3: Try looking at the initial answer and see what the problem is. Hint: the initial answer had a bug. – stackoverflowuser2010 Oct 31 '16 at 06:02
  • You mean this answer's original state? That produced a flat list, which the OP didn't want. What was the problem? – TigerhawkT3 Oct 31 '16 at 06:16
  • For my understanding this answer is wrong. it asks to append an array on an array but the shown solution with ```+``` is with lists. If arrays is the topic, I think numpy.array is meant, otherwise you simply can ask for list or list in list. If you use ```+``` on numpy.arrays, it will give back the sum of bot arrays (each array 4 elements, together 8 elements) in a new array(4 elements as sum). If I use ```append()``` it says its not possible. – Veritas_in_Numeris May 07 '22 at 08:33
49

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.

Manasvi Batra
  • 591
  • 7
  • 12
0

One way is to unpack both the lists:

a = [1, 2, 3]
b = [10, 20]

[*a,*b] 

#output 
[1, 2, 3, 10, 20]

if you want to use numpy then:

import numpy as np
a=np.array(a)
b=np.array(b)
list(np.append(a,b))

#output
[1, 2, 3, 10, 20]
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
-1
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]
    C0=C0.append(temp)
    return C

C0=Fiestel()
print (C0)

Try this, I think this is what you are looking for.

Issac Saji
  • 106
  • 6