Python Arrays


Note : The Python support for Arrays is not built-in, but alternatively Python lists can be utilised.

You can also search for these topics, python arrays and lists, append the python arrays, how to add the arrays using python, arrays and strings in python, python arrays and loops, python array sort ascending, methods to python arrays manipulation, max and list difference the arrays in python, python hash and heap arrays, difference, python array declaration with size and input, can python arrays hold different types and data type, how to use python concatenate arrays horizontally.

What is an Array?

A array is a particular variable, which has several values at a time.

If you have a list of items (for example a list of car names), it may look like that to store names in one variables :

car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"

But what if you want to loop around the names and find a particular car? What if you didn't have 3 names, but 300 ?

An array can have several values under one name, with a number of indexes. You can access these values.

Example :- Create an array containing car names :

cars = ["Ford", "BMW", "Toyoto"]

print(names)

Output :-

['Ford', 'BMW', 'Toyoto']

Note: You can utilise LISTS as ARRAYS but you will have to include a library, such as the NumPy library, for dealing with arrays in Python.


Access the Elements of an Array

In the index number, you refer to an array element.

Example 1 :- Get the value of the first array item :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

x = names[0]

print(x)

Output :-

Microsoft

Example 2 :- Modify the value of the second array item :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

names[1] = "Google"

print(names)

Output :-

['Microsoft', 'Google', 'Yahoo', 'Wikipedia']

You can also search for these topics, access last element of an array python, how to access elements of an array in python, how to access individual elements of an array in python, get the last element of an array in python, Example for Access the Elements of an Array.

The Length of an Array

Use len() function is used to return the array size (the number of elements in an array).

Example :- Return the number of items in the array of names :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

x = len(names)

print(x)

Output :-

4

Note : The array length is always the highest array index.

You can also search for these topics, find the length of an array in python, set the size of an array in python, how to define the length of an array in python, get the length of an array in python, how to get the length of an array in python, how to set the length of an array in python, how to count the length of an array in python, know the length of an array in python, print the size of an array in python, can you change the size of an array in python.

Looping Array Elements

You can use the for in loop statement to loop all array elements.

Example :- Print each item in the names array :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

for x in names:
  print(x)

Output :-

Microsoft
Apple
Yahoo
Wikipedia

You can also search for these topics, how to store values in array using for loop in python, loop through array python, python loop array with index, loop through list python, loop through list python, Example for python Looping Array Elements.

Adding Array Elements

To add an element to the array you can use the append() method.

Example :- Add one more element to the names array :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

names.append("Google")

print(names)

Output :-

['Microsoft', 'Apple', 'Yahoo', 'Wikipedia', 'Google']

You can also search for these topics, addition of array elements in python, sum of array elements in python hackerrank solution, add array values to dictionary python, add array elements in python key value, elements in python matrix, in python map, in python multiple, to array in python numpy, array add element at beginning python, python value, how to add array elements in python.

Removing Array Elements

To delete an item from the array, use the pop() method.

Example 1 :- Delete the second element of the names array :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

names.pop(1)

print(names)

Output :-

['Microsoft', 'Yahoo', 'Wikipedia']

You may also remove an item in the array by using the remove() method.

Example 2 :- Remove the value "Yahoo" element :

names = ["Microsoft", "Apple", "Yahoo", "Wikipedia"]

names.remove("Yahoo")

print(names)

Output :-

['Microsoft', 'Apple', 'Wikipedia']

Note : Only the first occurrence of that value can be removed by the list remove() method.

You can also search for these topics, remove multiple elements from array python, remove element from array python by index, remove value from numpy array python, empty the array python, delete array elements in python, remove first array element python, in python string, how to remove array element in python, Example for python removing array elements.

Array Methods

Python includes a collection of integrated methods that can be used on lists/arrays.

Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

Note : The Arrays are not supported by Python but alternatively Python Lists can be utilised.

You can also search for these topics, python array method between two value, python array methods count, method definition in python, python programs array methods, list the python array methods.