Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Lists as arrays

Syntax, Example Program, Creation, Operations | Python Programming

A list in Python is just an ordered collection of items which can be of any type. By comparison an array is an ordered collection of items of a single type.

Lists as arrays

The arrays is a data structure in which the elements are of same data type.

A list in Python is just an ordered collection of items which can be of any type. By comparison an array is an ordered collection of items of a single type.

The elements in the array are separated by comma and are enclosed within the square bracket. For example

arr = [10,20,30,40,50]

The arr can be represented by following figure


Fig. 3.9.1 Array representation

Here the values are arranged sequentially as follows –

arr[0] = 10

arr[1] = 20

arr[2] = 30

arr[3] = 40

arr[4] = 50

 

1. Creation of Arrays

We can create an array using the array name and list of elements. For example

arr = [10,20,30,40]

will create an array containing the elements 10,20,...,40. These elements can be represented using for loop. Following program represents the array creation and display of elements.

ArrayDemo.py

arr = [10,20,30,40] print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

Output


Another method of creation of Array

We can also create an array using following method

a = [i for i in range(10)]

 

2. Operations on Arrays

1. Appending a value

Using append() function we can add the element in the array at the end. For example

ArrayDemo1.py

arr = [10,20,30,40]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

arr.append(50)

print("Now The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

Now The elements is array are ...

10

20

30

40

50

>>> 

Thus we can see that value 50 is appended in the array.

2. Inserting the element in the list

autemele ed We can insert the value at any desired location using insert() function. The syntax is insert(index,value)

For example

ArrayDemo2.py

arr = [10,20,30,40]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

arr.insert(2,25)

print("Now The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

Now The elements is array are ...

10

20

30

40

>>> 

3. Extending the array

We can extend one array by joining another array to it. For that purpose the extend() function is used. The syntax is

extend(new_array)

For example

ArrayDemo3.py

arr = [10,20,30,40]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

new_arr = [50,60,70]

arr.extend(new_arr)

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

50

60

70

>>> 

4. Removing the element from the array

Any desired element can be deleted from the array using remove() method. The syntax is

remove(index_of_element)

For example

ArrayDemo4.py

arr = [10,20,30,40]

print("The elements is array are ...").

for i in range(len(arr)):

print(arr[i])

arr.remove(30)

print("Now The elements is array are ...").

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

Now The elements is array are ...

10

20

40

>>> 

5. Removing last element from array

For removing the last element from the array then pop() function is used.

Syntax

pop()

For example

ArrayDemo5.py

arr = [10,20,30,40]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

arr.pop()

print("Now The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

Now The elements is array are ...

10

20

30

>>> 

6. Reversing the elements of array

We can reverse the contents of the array using reverse() function

For example

ArrayDemo6.py

arr = [10,20,30,40]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

arr.reverse()

print("Now The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

Output

The elements is array are ...

10

20

30

40

Now The elements is array are ...

40

30

20

10

>>> 

7. Counting the occurrence of element in array

We can count the number of times the particular element appears in the array using the count method.

For example

ArrayDemo7.py

arr = [10,20,30,40,50,20,30,20]

print("The elements is array are ...")

for i in range(len(arr)):

print(arr[i])

print("The element 20 appears for ", arr.count(20)," times in array")

Output

The elements is array are ...

10

20

30

40

50

20

30

20

The element 20 appears for 3 times in array

 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Syntax, Example Program, Creation, Operations | Python Programming - Lists as arrays