Python - List

Lists is a collection of values separated by commas and enclosed within a square bracket ([]). To some extent, lists are similar to arrays in other programming language (like C, Java). One of the difference between them is that all the items belonging to a list can be of different data type. Lists are mutable, hence, they can be altered even after their creation.

The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting from 0 in the beginning and -1 from the end of the list. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

List Representation with index number

List Method

Example 1: Create list and perform the following functions.

nums = [20, 25, 11, 9, 50, 25] # similar values

list = ['abcd', 786 , 2.23, 'john', 70.2]  # dis-similar values
tinylist = [123, 'john']

print (list)            # Print complete list
print (list[0])         # Print first element of the list
print (list[1:3])       # Print elements starting from 2nd till 3rd 
print (list[2:])        # Print elements starting from 3rd element
print (list[:4])        # Print 1st four elements
print (nums[-1])        # Print the last number

print (tinylist * 2)    # Print list two times
print (list + tinylist) # Print concatenated lists
mis = [list, tinylist]  # Print concatenated lists

nums.append(5)          # Append object to the end of the list
nums.insert(2,100)      # Insert an item at the specified position
nums.extend([4,5,6])    # Extend list by appending elements from the given list
nums.remove(91)         # Remove first occurrence of value
nums.index(11)          # Return index of 11 i.e. 2
nums.count(25)          # Count occurrence i.e. 2
nums.clear()            # Clear the list
nums.pop()              # Remove and return item at index (default last)
nums.pop(2)             # Remove and return item at 2nd index
del nums[2:]            # All elements will be deleted from 2nd index to end
nums.sort()             # Arrange the elements in ascending order
nums.reverse()          # Reverse the list

print ("Min = ", min(nums))     # Display minimum value
print ("Max = ", max(nums))     # Display maximum value
print ("Sum = ", sum(nums))     # Display sum of all values
print ("Length = ", len(nums))  # Display list length

Nested List

Nested list means we can create a list inside another list.

Example 2: Initialize the 3x3 matrix and print it.

m = [ [10, 25, 3] , [14, 50, 66] , [17, 88, 59] ]

for i in m:
    for j in i:
        print(j, end="\t")

Example 3: Input nxm matrix and print them using list

r = int(input("Enter number of rows:"))
c = int(input("Enter number of columns:"))

# Declare empty matrix
matrix = []

# For user input
for i in range(r):		 # Loop for row entries
    a =[]
    for j in range(c):	 # Loop for column entries

# For printing the matrix
for i in range(r):
    for j in range(c):
        print(matrix[i][j], end = " ")

Exercise - Python List

  1. Find sum of all numbers in a list
  2. Find average of numbers in a list.
  3. Display largest number from a list
  4. Display smallest number from a list
  5. Input 10 numbers in a list and find sum of odd and even numbers separately.
  6. Input 10 numbers and reverse the list.
  7. Remove duplicate values from a list.
  8. Find factorial of each number stored in array.