Note: Python does not have inbuilt array support, but the can be used as an array.
The Array is a type of data container which is used to store multiple data elements in one variable. Elements of the array can be accessed using its index number.
Array (List) can be created by separating it's elements using comma (,) and enclosing them with square bracket [ ].
Info = ['John', 'Marry', 'Kim'] print(Info)
['John', 'Marry', 'Kim']
An element of a array can be accessed with it's index number. In Python, index number starts with 0 in forward direction and -1 in backward direction. The below figure and example describe the indexing concept of an array.
weekday = ['MON', 'TUE', 'WED', 'THU', 'FRI'] #forward indexing print(weekday[1]) #backward indexing print(weekday[-1])
TUE FRI
Range of elements of an array can be selected using statement like [start_index : end_index] where end_index is excluded. If start_index and end_index are not mentioned then it takes first and last index numbers of the array respectively.
weekday = ['MON', 'TUE', 'WED', 'THU', 'FRI'] print(weekday[1:3]) print(weekday[-5:-1]) print(weekday[1:]) print(weekday[:-3]) print(weekday[:])
['TUE', 'WED'] ['MON', 'TUE', 'WED', 'THU'] ['TUE', 'WED', 'THU', 'FRI'] ['MON', 'TUE'] ['MON', 'TUE', 'WED', 'THU', 'FRI']
To change element's value, assign new value using it's index.
Info = ['John', 'Marry', 'Kim'] #value at index=0 changed to 'Jo' Info[0] = 'Jo' print(Info)
['Marry', 'Jo', 'Kim']
Below mentioned methods are used to add elements in an array:
days = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'] # add this element in last of the array days.append('SUN') print(days) month = ['JAN', 'FEB', 'MAR', 'MAY'] # add 'APR' at index=3 of the array month.insert(3,'APR') print(month)
['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', SUN'] ['JAN', 'FEB', 'MAR', 'APR', 'MAY']
Below mentioned methods/keywords is used to delete elements from an array:
number = [10, 50, 50, 100, 1000] #delete first occurrence of 50. number.remove(50) print(number) number = [10, 50, 50, 100, 1000] #delete element at index=3. number.pop(3) print(number) number = [10, 50, 50, 100, 1000] #delete last element from the array. number.pop() print(number) number = [10, 50, 50, 100, 1000] #delete all elements from the array. number.clear() print(number)
[10, 50, 100, 1000] [10, 50, 50, 1000] [10, 50, 50, 100] []
The del keyword is used to delete an element or range of elements or array itself.
number = [10, 50, 50, 100, 1000] #delete element at index=3. del number[3] print(number) number = [10, 50, 50, 100, 1000] #delete element at index=1,2 and 3. del number[1:4] print(number) number = [10, 50, 50, 100, 1000] #delete array 'number'. del number print(number)
[10, 50, 50, 1000, 1000] [10, 1000, 1000] NameError: name 'number' is not defined
The len() function can be used to find out total number of elements in an array.
number = [10, 50, 50, 100, 1000, 1000] print(len(number))
6
for loop can be used to access each element of an array.
colors = ['Red', 'Blue', 'Green'] for x in colors: print(x)
Red Blue Green
By using while loop and len() function, each element of an array can be accessed.
colors = ['Red', 'Blue', 'Green'] i = 0 while i < len(colors): print(colors[i]) i = i + 1
Red Blue Green
If control statement is used to check whether the array contains specified element or not.
colors = ['Red', 'Blue', 'Green'] if 'white' in colors: print('Yes, white is an element of colors.') else: print('No, white is not an element of colors.')
No, white is not an element of colors.
Methods | Description |
---|---|
Adds a specified element to the end of the list | |
The | Deletes all elements from the list |
Array | Creates a copy of the list into a new list |
is a type of data container which is used to store multiple data elements in one variable. Elements of the array can be accessed using its index number. | Returns the number of occurrence of a specified element in the list |
Adds an element or iterable at the end of the list | |
Create an Array | Returns the index of first occurrence of a specified element in the list |
Adds a specified element at specified index in the list | |
Array (List) can be created by separating it's elements using comma (,) and enclosing them with square bracket [ ]. | Deletes an element at specified index or last element of the list |
Deletes first occurrence of a specified element in the list | |
Reverses order of all elements in the list | |
Example | Sorts elements of the list in ascending or descending order with specified sorting criteria |
Functions | Description |
Returns total number of elements in the list | |
Info = ['John', 'Marry', 'Kim'] print(Info) | List function/constructor is used to create list from iterable like list, tuple, set, string and dictionary, etc. |