>>> n = 5 #length of list
>>> list = [None] * n #populate list, length n with n entries "None"
>>> print[list]
[None, None, None, None, None]
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[None, None, None, None, 1]
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[None, None, None, 1, 1]
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[None, None, 1, 1, 1]
or with really nothing in the list to begin with:
>>> n = 5 #length of list
>>> list = [] # create list
>>> print[list]
[]
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[1]
on the 4th iteration of append:
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[1,1,1,1]
5 and all subsequent:
>>> list.append[1] #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print[list]
[1,1,1,1,1]
Hey, folks! In this article, we will be focusing on some Easy Ways to Initialize a Python Array. Python Array is a data structure that holds similar data values at contiguous memory locations. When compared to a
List[dynamic Arrays], Python Arrays stores the similar type of elements in it. While a Python List can store elements belonging to different data types in it. Now, let us look at the different ways to initialize an array in Python. Python for loop and range[]
function together can be used to initialize an array with a default value. Syntax:What is a Python array?
Method 1: Using for loop and Python range[] function
[value for element in range[num]]
Python range[] function accepts a number as argument and returns a sequence of numbers which starts from 0 and ends by the specified number, incrementing by 1 each time.
Python for loop would place 0[default-value] for every element in the array between the range specified in the range[] function.
Example:
arr=[] arr = [0 for i in range[5]] print[arr]
We have created an array — ‘arr’ and initalized it with 5 elements carrying a default value [0].
Output:
Method 2: Python NumPy module to create and initialize array
Python NumPy module can be used to create arrays and manipulate the data in it efficiently. The numpy.empty[] function creates an array of a specified size with a default value = ‘None’.
Syntax:
numpy.empty[size,dtype=object]
Example:
import numpy as np arr = np.empty[10, dtype=object] print[arr]
Output:
[None None None None None None None None None None]
Method 3: Direct method to initialize a Python array
While declaring the array, we can initialize the data values using the below command:
array-name = [default-value]*size
Example:
arr_num = [0] * 5 print[arr_num] arr_str = ['P'] * 10 print[arr_str]
As seen in the above example, we have created two arrays with the default values as ‘0’ and ‘P’ along with the specified size with it.
Output:
[0, 0, 0, 0, 0] ['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P']
Conclusion
By this, we have come to the end of this topic. Please feel free to comment below in case, you come across any doubt.
References
- Python array initialization — Documentation
To change the size of an array in Python, use the reshape method of the numpy library. reshape[a,d] The reshape function changes the size of the array without deleting the elements. Note. The new size must equal
the cardinality of the old array. For example, if a vector has 10 elements, it can be transformed into a 5x2 or 2x5 matrix. It cannot be made into a 3x3 matrix or anything else.
Example
Example 1 [vector to matrix]
Create an array of 10 elements using the array method.
import numpy as np
x=np.array[[1,2,3,4,5,6,7,8,9,10]]
The new array x has one size. It is a vector.
Change the array to a 5x2 array using the function reshape.
y=np.reshape[x,[5,2]]
The new array y has two dimensions.
>>> y
array[[[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]]]
It has the same elements as the vector x but arranged in a matrix.
Example 2
To get the same result, use reshape as the method.
y=x.reshape[[5,2]]
The end result is the same.
Example 3 [from matrix to vector]
Create a 2x5 matrix
import numpy as np
x=np.array[[[1,2,3,4,5],[6,7,8,9,10]]]
The array x has two dimensions
>>> x
array[[[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]]]
Transform the matrix into a vector.
z=np.reshape[x,10]
The new array has one size.
>>> z
array[[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
//how.okpedia.org/en/python/how-to-change-array-size-in-python