Anamika Jain Anamika Jain - 1 year ago 101
Python Question

How to store a part of an array in different array in python

Let's say I have an array

A=[1 2 3 2 4 5 6]


Now i need to store first 3 values of array A into array B

I am doing

b.append(a[1])
b.append(a[2])
b.append(a[3])


but I am unable to get any output.

Answer Source

You should consider to use slices

a = [1, 2, 3, 4, 5]
b = a[:3]

print b #print(b) for Python 3.x

Output:

[1, 2, 3]

https://docs.python.org/2/tutorial/introduction.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download