Functions of lists in Python

Lists: Lists are used to store multiple items (may be of same type elements ie homogeneous data types or different types elements ie heterogeneous data types) in a single variable. It is a comma separated list of values enclosed in square brackets [ ]. Example:

list1=[2,3,4,5]
#list of homogeneous elements

list2=[“gargs”, 100, 24.56 ]
#list of heterogeneous elements

list3=[“gargs” , “academy” , “Sonipat” , 9467863365]
#list of heterogeneous elements

list4=[]
#empty list

Creating Lists: We can create both empty lists as well as non empty lists by
1) directly assigning the literals/values and
2) by using the built in method list() of python.

Creating Empty Lists by directly assigning: We can create empty lists by assigning as:

>>> list1=[]
>>> list1
[]

Creating Empty Lists in Python using list(): We can create empty list using list() in various ways as

>>> list2=list()
>>> list2
[]

>>> list3=list([])
>>> list3
[]

>>> list4=list(())
>>> list4
[]

>>> list5=list({})
>>> list5
[]

Creating Non Empty Lists in Python by assigning: We can create non empty list in various ways as

>>>#Creating homogeneous list
>>> list1=[4,5,6,7]
>>> list1
[4, 5, 6, 7]

>>>#Creating heterogeneous list
>>> list2=["garg", 100,45.67]
>>> list2
['garg', 100, 45.67]

Creating Non Empty Lists in Python using list(): We can create non empty list in various ways as

>>>#creating list using list
>>> list1=list([4,5,6,7])
>>> list1
[4, 5, 6, 7]

>>>#creating list using tuple
>>> list2=list(("garg", 100,45.67))
>>> list2
['garg', 100, 45.67]

>>>#creating list using string
>>> list3=list("gargs acdaemy")
>>> list3
['g', 'a', 'r', 'g', 's', ' ', 'a', 'c', 'd', 'a', 'e', 'm', 'y']

>>>#creating list using set
>>> list4=list({200,200,200,100,300})
>>> list4
[200, 100, 300]

>>>#creating list using keys of dictionary
>>> list5=list({1:2,3:4,5:4})
>>> list5
[1, 3, 5]

Mutable: Lists are mutable objects ie. once created, we can change, increase/decrease the elements of the list, without changing its base address.

>>> list1=[3,4,5]

>>>list1

[3, 4, 5]

>>> id(list1)
2428427644416

>>> list1[2]=50

>>> list1
[3, 4, 50]

>>> id(list1)
2428427644416

>>> list1.append(20)

>>> list1
[3, 4, 50, 20]

>>> id(list1)
2428427644416

>>> list1.remove(4)

>>> list1
[3, 50, 20]

>>> id(list1)
2428427644416

Indexing on Python Lists: We can access any element using its index as:
listname[valid_index]
Python supports 2-way indexing on lists: Forward Indexing and Backward Indexing.

Forward Indexing: In it, list elements are indexed from 0 to n-1 where n is the number of elements in the list.

>>> list1=[3,4,5,1]

>>> list1[0]
3

>>> list1[3]
1

If the index does not exist, then an error message is shown as:

>>> list1=[3,4,5,1]

>>> list1[4]
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    list1[4]
IndexError: list index out of range

Backward Indexing: In it, lists are indexed from right side using negative indexes ie -1, -2, -3 and so on upto -n where n is the number of elements in the list.

>>> list1=[3,4,5,1]

>>> list1[-1]
1

>>> list1[-4]
3

If the index does not exist, then an error message is shown as:

>>> list1=[3,4,5,1]

>>> list1[-5]
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    list1[-5]
IndexError: list index out of range

List Operations: There are 6 types of operations possible on lists.
1. Concatenations
2. Repetition
3. Membership
4. Slicing
5. Traversing Lists
6. Manipulating Lists

Concatenation/ Merging/Joining two lists: We can join two lists as:
listname1 + listname2

>>> list1=[2,3,5]
>>> list1
[2, 3, 5]

>>> list2=[1,4,5]
>>> list2
[1, 4, 5]

>>> list1 + list2
[2, 3, 5, 1, 4, 5]

>>> list2 + list1
[1, 4, 5, 2, 3, 5]

Elements of both the lists will appear in the order, in which lists are written i.e list1 + list2 will display elements of lists1 before elements of list2 but list2 + list1 will display elements of lists2 before elements of list1

>>> list1 + list2
[2, 3, 5, 1, 4, 5]

>>> list2 + list1
[1, 4, 5, 2, 3, 5]

We can concatenate only a list with another list, otherwise python will report an error as:

>>> list1=[2,3,5]

>>> list1 + 7
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    list1 + 7
TypeError: can only concatenate list (not "int") to list

Repetition: We can replicate or repeat the list multiple times as:
1) n * listname
2) listname *n

>>> list1=[3,4,5]
>>> list1*2
[3, 4, 5, 3, 4, 5]
>>> list1*3
[3, 4, 5, 3, 4, 5, 3, 4, 5]

To repeat or replicate the list, we can also write as:

>>> list1=[3,4,5]
>>> 2*list1
[3, 4, 5, 3, 4, 5]
>>> 3*list1
[3, 4, 5, 3, 4, 5, 3, 4, 5]

Membership on Lists: In Python, there are two membership operators. (in, not in), which are used to test whether or not the value is present in the data sequence.
1) item in listname
2)item not in listname

>>> list1=[2,3,5]

>>> 2 in list1
True

>>> 4 in list1
False

>>> 2 not in list1
False

>>> 4 not in list1
True

in operator returns True only if the element is present as an individual element in the list

>>> list1=[2,3,[4,5]]

>>> 4 in list1
False

>>> [4,5] in list1
True

Slicing the lists: To access a range of elements from a list, we can extract a part/slice of the list as:
listname[initial : final : step]
It will extract a part of the list from initial position upto final position (final position not included), in given step size.
default value of initial is 0, default value of final is n and default value of size is 1

>>> list1=[3,4,5,6,7,8,9]

>>> list1[2:6:1]             #starting from 2 upto 6 in steps of 1
[5, 6, 7, 8]

>>> list1[2:6]               #starting from 2 upto 6 in default steps i.e. 1
[5, 6, 7, 8]

>>> list1[2:6:2]             #starting from 2 upto 6 in steps of 2
[5, 7]

>>> list1[0:]                #starting from 0 upto default ie n=7 in default steps ie 1
[3, 4, 5, 6, 7, 8, 9]

>>> list1[:]                 #starting from default ie 0 upto default ie n=7 in default steps ie 1
[3, 4, 5, 6, 7, 8, 9]

>>> list1[:7]                #starting from default ie 0 upto 7 in default steps ie 1
[3, 4, 5, 6, 7, 8, 9]

>>> list1[:10]               #starting from default ie 0 upto 10 in default steps ie 1
[3, 4, 5, 6, 7, 8, 9]

>>> list1[::]                #starting from default ie 0 upto default ie n=7 in default steps ie 1
[3, 4, 5, 6, 7, 8, 9]

>>> list1[::-1]              #starting from default ie 0 upto default ie n=7 in steps of -1            
[9, 8, 7, 6, 5, 4, 3]        #notice the list is reversed

>>> list1[]                  #atleast a : is necessary 
SyntaxError: invalid syntax  #even if you dont specify any initial, final or step value
                             #otherwise error message will be shown as

Traversing a list using loops: We can traverse (ie. access or print or perform any other operation on each individual value of the list exactly once) the lists using loops as:

>>> list1=[2,4,5,6]
>>> for val in list1:
	print(val)
	
2
4
5
6
>>> list1=[2,4,5,6]
>>> sum=0
>>> for val in list1:
	sum=sum+val

	
>>> print(sum)
17
>>> avg=sum/len(list1)
>>> print(avg)
4.25

Manipulating Lists: We can change the elements of the list as:
listname[validindex]=newvalue

>>> list1=[3,4,5,6,9]
>>> list1
[3, 4, 5, 6, 9]
>>> list1[3]=50
>>> list1
[3, 4, 5, 50, 9]

If the index is not valid, python will give error as:

>>> list1=[3,4,5,6,9]

>>> list1
[3, 4, 5, 6, 9]

>>> list1[5]=78
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    list1[5]=78
IndexError: list assignment index out of range

We can assign a new value for multiple indexes as:
listname[start:stop:step]=newvalue
default value for start is 0
default value for stop is n where n is the number of elements in the list.
default value of step size is 1

>>> list1=[3,4,5,2,3,4,6,7,8]
>>> list1[2:6:1]=[50]
>>> list1
[3, 4, 50, 6, 7, 8]

If we skip step size, then default step size ie 1 is taken.

>>> list1=[3,4,5,2,3,4,6,7,8]
>>> list1[2:6]=[50]
>>> list1
[3, 4, 50, 6, 7, 8]

We can also replace a part of list with another list as:

>>> list1=[3,4,5,2,3,4,6,7,8]

>>> list1[2:6]=[10,20,30,40]

>>> list1
[3, 4, 10, 20, 30, 40, 6, 7, 8]
>>>list1=[3, 4, 10, 20, 30, 40, 6, 7, 8]
>>> list1
[3, 4, 10, 20, 30, 40, 6, 7, 8]

>>> list1[2:6]=[100,200,300]
>>> list1
[3, 4, 100, 200, 300, 6, 7, 8]

del : It is used to delete an element with given valid index position from a list or delete the list entirely.

>>>list1=[3, 4, 50, 6, 7, 8]
>>> list1
[3, 4, 50, 6, 7, 8]

>>> del list1[2]

>>> list1
[3, 4, 6, 7, 8]

If the index does not exist, python will give error as:

>>>list1=[3, 4, 50, 6, 7, 8]

>>> list1
[3, 4, 50, 6, 7, 8]

>>> del list1[20]
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    del list1[20]
IndexError: list assignment index out of range

Del is also used to delete the list entirely. After deleting the entire list, we will no longer be able to use the list.

>>>list1=[3, 4, 6, 7, 8]

>>> list1
[3, 4, 6, 7, 8]

>>> del list1

>>> list1
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    list1
NameError: name 'list1' is not defined

Applying built in functions on Lists in Python: Built in functions are predefined functions in python that are available for use without the need for importing any module. We can apply various built in functions on lists such as len(), list(), sum(), min(), max(), type(), id(), sorted(), del() etc.

len() is used to find the number of elements of the list.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> len(list1)
5

>>> list2=[]
>>> list2
[]

>>> len(list2)
0

list() : It is used to create lists.

>>> list1=[]
>>> list1
[]

>>> list2=list()
>>> list2
[]

>>> list3=list([])
>>> list3
[]

>>> list4=list(())
>>> list4
[]

>>> list5=list({})
>>> list5
[]

>>> list6=list("gargs acdaemy")
>>> list6
['g', 'a', 'r', 'g', 's', ' ', 'a', 'c', 'd', 'a', 'e', 'm', 'y']

sum() is used to find the sum of elements of the list.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> sum(list1)
28

If the lists elements are incompatible for addition, then an error message is shown as:

>>> list2=["garg","academy",9467863365]

>>> sum(list2)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    sum(list2)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

min() is used to find the minimum from the elements of the list.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> min(list1)
4

If the lists elements are incompatible for finding minimum, then an error message is shown as:

>>> list2=["garg","academy",9467863365]

>>> min(list2)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    min(list2)
TypeError: '<' not supported between instances of 'int' and 'str'

max() is used to find the maximum from the elements of the list.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> max(list1)
7

If the lists elements are incompatible for finding maximum, then an error message is shown as:

>>> list2=["garg","academy",9467863365]

>>> max(list2)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    max(list2)
TypeError: '>' not supported between instances of 'int' and 'str'

type() is used to find the data type of the object passed as an argument.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> type(list1)
<class 'list'>

id() is used to find the address or location where the first element of the list is stored in memory.

>>> list1=[4,5,6,7,6]
>>> list1
[4, 5, 6, 7, 6]

>>> id(list1)
2253257881728

sorted(): It is used to create a list from the elements of a given list in sorted order.

>>> list1=[3,4,2,1]

>>> sorted(list1)
[1, 2, 3, 4]

We can also store the result in a list variable as:

>>> list1=[3,4,2,1]
>>> list2=sorted(list1)

>>> list2
[1, 2, 3, 4]

But the original list remains unchanged.

>>> list1
[3, 4, 2, 1]

del() : It is used to delete an element with given valid index position from a list or delete the list entirely.

>>>list1=[3, 4, 50, 6, 7, 8]
>>> list1
[3, 4, 50, 6, 7, 8]

>>> del (list1[2])

>>> list1
[3, 4, 6, 7, 8]

If the index does not exist, python will give error as:

>>>list1=[3, 4, 50, 6, 7, 8]

>>> list1
[3, 4, 50, 6, 7, 8]

>>> del (list1[20])
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    del (list1[20])
IndexError: list assignment index out of range

Del() is also used to delete the list entirely. After deleting the entire list, we will no longer be able to use the list.

>>>list1=[3, 4, 6, 7, 8]

>>> list1
[3, 4, 6, 7, 8]

>>> del (list1)

>>> list1
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    list1
NameError: name 'list1' is not defined

Lists Functions in Python: We can apply various functions on lists such as:

  1. append()
  2. extend()
  3. insert()
  4. count()
  5. index()
  6. remove()
  7. pop()
  8. reverse()
  9. sort()
  10. clear()
  11. copy()

To see a list of all functions of a list:

>>> dir(list)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

To see a description of all the functions of the list:

>>> help(list)
Help on class list in module builtins:

class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
 |  
 |  If no argument is given, the constructor creates a new empty list.
 |  The argument must be an iterable if specified.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(self, /)
 |      Return a reverse iterator over the list.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(self, /)
 |      Return the size of the list in memory, in bytes.
 |  
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |  
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  insert(self, index, object, /)
 |      Insert object before index.
 |  
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |      
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |  
 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |      
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |      
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |      
 |      The reverse flag can be set to sort in descending order.
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  __class_getitem__(...) from builtins.type
 |      See PEP 585
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

append(): To add an item into a list. The item may be an integer, float, string, list, tuple, dictionary etc.
listname.append(item)

>>> list1=[4,5,6,7,6]

>>> list1.append(10)
>>> list1
[4, 5, 6, 7, 6, 10]

If we give only a single item, but in a list form, it will be added as a list item as:

>>> list1=[4,5,6,7,6,10]

>>> list1.append([20])
>>> list1
[4, 5, 6, 7, 6, 10, [20]]

If we give 2 items separated by comma, then it will give error.

>>> list1=[4,5,6]
>>> list1.append(30,40)
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    list1.append(30,40)
TypeError: list.append() takes exactly one argument (2 given)

If we give 2 items separated by comma in a list from, then it will be added as a single element in the list.

>>> list1=[4,5,6]
>>> list1.append([30,40])
>>> list1
[4, 5, 6, [30, 40]]

extend() : To extend or add a list (having one or more elements) at the end of the given list.
listname.extend(list2)

>>> list1=[3,4,5]

>>> list1.extend([10,20,30])
>>> list1
[3, 4, 5, 10, 20, 30]

>>> list1.extend([40])

>>> list1
[3, 4, 5, 10, 20, 30, 40]

But the element to be extended must be a list. Otherwise an error message will be shown as:

>>> list1=[3,4,5]

>>> list1.extend(50)
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    list1.extend(50)
TypeError: 'int' object is not iterable

>>> list1.extend(50,60)
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    list1.extend(50,60)
TypeError: list.extend() takes exactly one argument (2 given)

insert(): It is used to insert an element at the given location in the list.
list1.insert(position , value)

>>> list1=[10,20,30]
>>> list1.insert(2,40)
>>> list1
[10, 20, 40, 30]

If the location is negative, it will insert as:

>>> list1=[4,5,6]

>>> list1.insert(-1,10)
>>> list1
[4, 5, 10, 6]

>>> list1.insert(-2,20)
>>> list1
[4, 5, 20, 10, 6]

If the location is not valid and location is positive, then item will be added as the last element in the list.

>>> list1=[10,20,30]

>>> list1.insert(10,50)
>>> list1
[10, 20, 30, 50]

If the location is not valid and location is negative, then item will be added as the first element in the list.

>>> list1=[10,20,30,50]

>>> list1.insert(-8,60)
>>> list1
[60, 10, 20, 30, 50]

count(): To count the frequency of an item in the list.

>>> list1=[4,5,6,7,6]
>>> list1.count(6)
2

If the element does not exist in the list, it returns 0

>>> list1=[4,5,6,7,6]
>>> list1.count(30)
0

index(): It is used to find the index position of first occurrence of an item in the list.

>>> list1=[3,4,5,4,3]

>>> list1.index(4)
1

If the item is not present in the list, it will result into an error as:

>>> list1=[3,4,5,6,3]

>>> list1.index(30)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    list1.index(30)
ValueError: 30 is not in list

remove(): It is used to remove the first occurrence of an item from the list.

>>> list1=[3,4,5,4,3]

>>> list1.remove(3)
>>> list1
[4, 5, 4, 3]

If the item is not present in the list, It will report an error as:

>>> list1=[3,4,5,4,3]

>>> list1.remove(30)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    list1.remove(30)
ValueError: list.remove(x): x not in list

pop(): It is used to remove/pop the item with given index position from the list. If we don’t specify any index position, then last item of the list will be popped.

>>> list1=[3,4,5,4,3]

>>> list1.pop(3)
4

>>> list1
[3, 4, 5, 3]

>>> list1.pop(0)
3

>>> list1
[4, 5, 3]

If we don’t specify any index position, then last item of the list will be popped.

>>> list1=[4,5,3]

>>> list1.pop()
3

>>> list1
[4, 5]

reverse(): It is used to reverse the list.
listname.reverse()

>>> list1=[3,4,5,2]

>>> list1.reverse()

>>> list1
[2, 5, 4, 3]

sort(): It is used to arrange the list in ascending or descending order.
listname.sort(reverse=True/False)
default value of reverse is False

>>> list1=[3,4,5,2]

>>> list1.sort(reverse=False)
>>> list1
[2, 3, 4, 5]

>>> list1.sort(reverse=True)
>>> list1
[5, 4, 3, 2]

>>> list1.sort()
>>> list1
[2, 3, 4, 5]

If the lists elements are incompatible for sorting, then an error message is shown as:

>>> list2=["garg","academy",9467863365]

>>> list2.sort()
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    list2.sort()
TypeError: '<' not supported between instances of 'int' and 'str'

clear(): It is used to delete all the elements of a list.

>>>list1=[3,4,5,9]
>>> list1.clear()
>>> list1
[]

copy(): It is used to create a copy of a list.

>>> list1=[4,5]

>>> list2=list1.copy()
>>> list2
[4, 5]

Nested Lists: We can create a list having another lists as its elements as:

>>> list1=[3,4,[5,6,7,4],3]

>>> list1
[3, 4, [5, 6, 7, 4], 3]

We can create as many levels of nesting as we want as shown :

>>> list1=[3,4,[5,6,7,[5,6,9],4],3]

>>> list1
[3, 4, [5, 6, 7, [5, 6, 9], 4], 3]

We can access elements of nested lists by using multiple indexes as:

>>> list1[0]
3

>>> list1[2][0]
5

>>> list1[2][3][1]
6

Types of Nested Lists: There are 2 types of nested lists:
1) Regular Lists
2) Ragged Lists

Regular Lists: These are the lists whose all elements have same shape/size/number of elements. Example

>>> list1=[[2,3,4],[4,5,6],[7,8,9]]
>>> list1
[[2, 3, 4], [4, 5, 6], [7, 8, 9]]

Ragged Lists: These are the lists whose all elements don’t have same shape/size/number of elements. Example

>>> list1=[[2,3,4],[4,5,6],[7,8]]
>>> list1
[[2, 3, 4], [4, 5, 6], [7, 8]]

Suggested programs: Program 1: finding the maximum, minimum, mean of numeric values stored in a list

list1=eval(input("Enter the list"))
print("The list is")
print(list1)

mx=max(list1)
print("The maximum no in the list is" ,mx)

mn=min(list1)
print("the minimum no in the list is",mn)

sum=0
for val in list1:
    sum=sum + val
print("The sum of the numbers in the list is", sum)
n=len(list1)
avg=sum/n
print("the average of the numbers in the list is" , avg) 

Output :

Enter the list[3,4,5,2]
The list is
[3, 4, 5, 2]
The maximum no in the list is 5
the minimum no in the list is 2
The sum of the numbers in the list is 14
the average of the numbers in the list is 3.5

Suggested programs: Program 2: linear search on list of numbers

list1=eval(input("Enter the list"))
no=int(input("Enter the number to find"))
print("The list is")
print(list1)
n=len(list1)
for i in range(n):
    if no==list1[i]:
       print(no , " Found at location", i+1)
       break
else:
    print(no, " Not found in the list")

Output 1:

Enter the list[3,4,5,3]
Enter the number to find3
The list is
[3, 4, 5, 3]
3  Found at location 1

Output 2:

Enter the list[3,4,5,3]
Enter the number to find2
The list is
[3, 4, 5, 3]
2  Not found in the list

Suggested programs: Program 3: counting the frequency of elements in a list

list1=eval(input("Enter the list"))
no=int(input("Enter the number to find frequency"))
print("The list is")
print(list1)
n=list1.count(no)
print(no, " appears in the list " ,n , " times")

Output 1:

Enter the list[3,4,5,3,2,3,1,3]
Enter the number to find frequency3
The list is
[3, 4, 5, 3, 2, 3, 1, 3]
3  appears in the list  4  times

Output 2:

Enter the list[3,4,5,5]
Enter the number to find frequency50
The list is
[3, 4, 5, 5]
50  appears in the list  0  times
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.