Array From numpy
Array is not supported in Python out of the box. List can be used as an Array using NumPy library.
Array store multiple values in a single variable.
Array elements can be accessed using index number.
Example:
array1 = [“HTML”, “CSS”, “Java”];
Array elements can be access using index, it always starts from 0.
e.g. →
Define an Array with four elements HTML, CSS, JavaScript and Java as show below:
arrayOne = [“HTML”, “CSS”, “JavaScript”, “Java”];
arrayOne[0] → HTML,
arrayOne[1] → CSS,
arrayOne[3] → Java
Print Array element
We can use for loop to print an Array.
languages = ["HTML", "CSS", "Java", "Javascript"];for language in languages:
print(language)
OUTPUT:
HTML
CSS
Java
Javascript
Example: Below example is all about access, update and delete list items.
arrayOne = ["HTML", "CSS", "JavaScript", "Java"];
# Access an element at 0th index
print(arrayOne[0]);
# Updating an element at particular index in Array
arrayOne[1] = "CSS3";
# Print Array items
print(arrayOne);
# Add element in an Array
arrayOne.append("C++");
print(arrayOne);
# this will return the length of an Array
print(len(arrayOne));
#Remove an Array item at index 2
arrayOne.remove("C++");
print(arrayOne);
#Return index of give element
print(arrayOne.index("Java"));
#remove last inserted value
print("Remove Element: ", arrayOne.pop());
# Print Array items again
print(arrayOne);
OUTPUT:
HTML
['HTML', 'CSS3', 'JavaScript', 'Java']
['HTML', 'CSS3', 'JavaScript', 'Java', 'C++']
5
['HTML', 'CSS3', 'JavaScript', 'Java']
3
Remove Element: Java
['HTML', 'CSS3', 'JavaScript']
There are some other Array functions supported by NumPy Python library as given below:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.