List in Python

Arrays in C 

Array in C programming language is a collection of similar typed elements

For Example:

int a[3] = {10, 20, 30}
float b[3] = {10.5, 20.5, 30.0}

Video Tutorial

Definition of List in Python

List in python is a collection of similar or heterogeneous typed elements

For Example:

L1 = [10, 20, 30]
L2 = [10, 20, 30.5]
L3 = [10, 20.3, 'VTU', 'BGM']
L4 = list(10, 20, 30)

Here the list L1 contains three integers typed elements.

Here the list L2 contains two integers typed elements and one floating-point element.

Difference between arrays and list

Difference between arrays and List.

  1. In arrays, we can store only similar typed elements but in the list, we can store both similar and heterogeneous typed elements
  2. The size of the array is fixed, that is once the size of the array is defined, it cannot be altered, but the size of the list can be altered as the need arises. 
  3. The list can have list as an element.

Accessing Elements of List

The elements of the list are accessed using numeric indexes.

The index of the first element always starts at 0.

The syntax uses the bracket operator. 

See also  Python program that stores the student name, roll number, and marks in three subjects

The expression inside the brackets specifies the index. 

Example:

L1 = [10, 20, 30]
print (L1[0])

    The output of this statement is 10.

Similarly,

print(L1[1])

    Outputs the value 20.

print(L1[2])

    Outputs the value 30.

Rules for using Index:

  • Any integer expression can be used as an index.
  • If you try to read or write an element that does not exist, you get an IndexError.
  • If an index has a negative value, it counts backward from the end of the list.

Example:

L1 = [10, 20, 30]
print (L1[1+1])

The output of this statement is 30.

print(L1[-1])

Outputs the value 30. Returns the last element of the list. 

print (L1[4])

Output IndexError, as the list L1 contains only 3 elements, indexed at 0, 1, and 2.

Lists are Mutable

The elements of the list can be modified or rearranged as the need arises.

For Example:

L1 = [10, 20, 30] 
L1[1] = 200

    Now the value present at index 2 is 200 in place of 20. 

Sample programs to demonstrate the lists are mutable

l1 = [10, 20, 30]

print ("Before Modificaton:", l1)

l1[0] = 50
l1[1] = 60
l1[2] = 80

#l1[0:3] = [50, 60, 80]

print ("After Modificaton:", l1)

Output

Before Modificaton: [10, 20, 30]
After Modificaton: [50, 60, 80]

print statement can be used to print all the elements.

See also  Python program extract decimal number in the range of 00-99

Example:

print(L1)

    Output: 10, 200, 30

In operator on Lists

The in operator is used to check whether the queried element is present in the list or not. 

If the element is present then in operator returns True otherwise False.

Example:

L1 = [10, 20, 30] 
10 in L1

        Returns True as the element 10 is present in L1

100 in L1

        Returns False as element 100 is not present in L1

Traversing a List

There are two methods to traverse the elements of the list in python.

In the first method, you can use for loop to iterate over the list. In each iteration, one element of the list is returned.

Example:

numbers = [10, 20, 15] 
for ele in numbers: 
   print(ele)

This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices.

In the second method, you can use the range and len function to traverse the list elements.

See also  Python program to count the frequency of each character in the string

Example:

numbers = [10, 20, 15] 
for i in range(len(numbers)):
    print(numbers[i])

len returns the number of elements in the list. range returns a list of indices from 0 to n − 1, where n is the length of the list. Each time through the loop, i get the index of the next element.

Example:

#Multiplication by 2 (each element)

l1 = [10, 20, 30]
print ("Before Modification:", l1)
for i in range(len(l1)):
    l1[i] = l1[i] * 2
print ("After Modification:", l1)

Output

Before Modification: [10, 20, 30]
After Modification: [20, 40, 60]

Program to find the sum of elements of a list

#Sum of list elements
sum = 0
l1 = [10, 20, 30]

for ele in l1:
    sum = sum + ele
print ("sum of ", l1, "is", sum)

Output

sum of  [10, 20, 30] is 60

This loop traverses the list and updates each element.

Traversing an empty List

A for loop over an empty list never executes the body:

for x in empty:
   print('This never happens.')

Summary:

This tutorial discusses the usage of List in Python. If you like the tutorial do share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *