In this article you will learn about Python arrays.
Python Arrays – Before moving ahead, let’s know a little bit about Python String
Arrays – It is used to hold any number of values in one variable. However, Python doesn’t not built-in support of arrays, but a list can be used as an array. Using an array is quite simple, Python needs to import its library for this.
Example 1- Creating an array.
x =['ab', 'cd', 'ef', 'gh'] print(x)
Explanation – In the above example, it is shown clearly that it returned specified string of specified index number.
Note: An index number in Python starts from 0.
Access items – An array item can be accessed by referring index number of specified items.
Example 2- Get the item at the position 1 by use of index() method.
x =['ab', 'cd', 'ef', 'gh'] print(x[1])
Explanation – In the above example, it is shown clearly that it returned specified string of specified index number.
Change item – An array item can be changed by referring an index number of specified item.
Example 3- Change the item of an array by referring an index number.
x =['ab', 'cd', 'ef', 'gh'] x[1] = 'ij' print(x)
Explanation – In the above example, it is shown clearly that it inserted specified string at the specified index number.
Length of array – To get the length or number of items of an array use len() function.
Example 4- Use of len() function to get the length.
x =['ab', 'cd', 'ef', 'gh'] print(len(x))
Explanation – In the above example, it is shown clearly that it returned the length of the string.
Looping array items – To loop through all items of an array use ‘for’ loop.
Example 5- Get each item of an array by using loop.
z =['ab', 'cd', 'ef', 'gh'] for x in z: print(x)
Explanation – In the above example, it is shown clearly that it returned elements of an array on by one after looping every elements.
Adding items – To add an item in the array use append() method.
Example 6- Adding an item ‘as’ in the array.
x =['ab', 'cd', 'ef', 'gh'] x.append('as') print(x)
Explanation – In the above example, it is shown clearly that, by default append() method added an element in the last position.
Removing items – There are two methods to remove items from the array.
pop() method – To remove an item of array use pop() method. By default, it removes the last item of the array. Or item can also be removed by referring an index number.
Example 7- Use of pop() method to delete last item of the array.
x =['ab', 'cd', 'ef', 'gh'] x.pop() print(x)
Explanation – In the above example, it is shown clearly that by default pop() method deleted last item of the array.
Example 8- Use of pop() method to delete an item of the array by referring an index number.
x =['ab', 'cd', 'ef', 'gh'] x.pop(1) print(x)
Explanation – In the above example, it is shown clearly that it deleted specified string at specified index number.
remove() method – To remove an item from the array use remove() method.
Example 9- Use of remove() method to remove an item of the array.
x =['ab', 'cd', 'ef', 'gh'] x.remove('ef') print(x)
Explanation – In the above example, it is shown clearly that it removed specified element from list.
Note: list’s remove() method only removes first occurrence of the specified value.
If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.
Like us on