Array Operations in Python

In Python an array is known under the name List. List is a collection of comma-separated values of any data type, enclosed within square brackets. The values are subject to changes. Thus they are called mutable.

1. Representation of lists

Look at the example,

a = [1, 2, 3, 4] 

Here a list ‘a’ is created and values 1,2,3,4 are stored in the list.

To display the contents of a list.

print(a)

Displays the contents of the list ‘a’

Output: 

[1, 2, 3, 4]

2. Accessing individual elements

Index is used to access individual elements. Index starts from 0.

Example:  

b = [1, 2, 3, 6, 7, 8]

print(b[0])

print(b([3])

Output:

  1

  6

Example:

b = [1, 2, 3, 6, 7, 8]

print(b[-4])

Output: 3

3. Elements are Mutable

The individual elements can be changed , and thus it is called as mutable.

Take the following example

b = [2, 4, 6, 8, 10]

b[3] = 20

print(b)

Output:

[2,4,6,20,10]

Explanation: The element at the index 3, is replaced with the new value.

4. Slicing operations on Lists

Slicing can be done by specifying the position from where it is to be sliced. This is done using the : (Colon) operator. The general format is newlist = list[start:stop]

Example: 

list = [1,2,3,4,5,6,7,8]

newlist = list[3:6]

print(newlist)

Output:

[4, 5, 6, 7]  

Explanation :  Here the list is sliced at position three and the new list is created leaving an element from the last position.

Example:  b = [1, 2, 3, 4, 5, 6, 7, 8]

print( b[-3:])

Output: 

[6, 7, 8]

Explanation: The last three elements of the List are sliced and displayed.

5. Slicing and changing the values

Example:

list = [“a”, “b”, “c”’]

print(list)

Output:

[‘a’, ‘b’, ‘c’]

Explanation: Complete list will be printed.

Example:

list[2] = “k”

print(list)

Output:

[‘a’,’k’,’c’]

6. Eliminating a part of the list

Example:

list = [‘a, ‘b’, ‘c’, ‘d’, e’, ‘f’, ‘g’, ‘h’]

list[2:5] = [‘C’,’D’,’X’]

print(list)

Output:

[‘a, ‘b’, ‘C’, ‘D’, X’, ‘f’, ‘g’, ‘h’]

Example:

list[2:5] = []

print(list)

Output:

[‘a’, ‘b’, ‘f’, ‘g’, ‘h’]

Explanation: The characters from the index position 3 to 5 (included) will be eliminated.

7. Clearing the list

Example: 

list = [‘a’, ‘b’, ‘f’, ‘g’, ‘h’]

list[:] = []

print(list)

Output:

[]

The list is cleared completely.

8. Copy of the list

Example:

list = [1, 2, 3, 4, 5, 6, 7, 8]

print(list[:])

Output

[1,2,3,4,5,6,7,8]

9. Concatenation of Lists

‘+’ operator is used for joining two lists.

Example: 

list1 = [1,2,3,4]

list2 = list1 + [10,20,30,40]

print(list2)

Output:

The elements are added at the end (append)

[1,2,3,4,10,20,30,40]

Example: 

list1 = [1,2,3]

list2 =  [4,5,6]

list3 = list1 + list2

print(list3)

Output:

[1,2,3,4,5,6]

10. Method append()

We can add elements to the end of a list using the method append(). The argument can be a value or an expression

Example: 

list = [1,2,3,4,5]

list.append(8)

print(list)

Output:

[1,2,3,4,5,8]

Example: 

list = [1,2,3,4,5]

list.append(5 * 2)
print(list)

Output:

[1,2,3,4,5,10]

11. len()

Built-in function len() returns the length of a list

Example:

list2 = ["a","b","c","d"]

print(len(list2))

Output:

    4

12. Nested lists

Nesting of lists is one list existing inside another list

Example:

plist = ['a', 'b', 'c', ‘d’]

qlist = [30, 40, 50, 60]

rlist  = [plist, qlist]

print(rlist)

Output:

[[‘a’, ’b’, ’c’, ’d’], [30, 40, 50, 60]]

 Example:

print(rlist[0][2])

Output:

   c