Skip to content
cs2study

cs2study

Computer Science C.B.S.E syllabus

  • Home
  • Python-Class XI
    • Videos
    • PDF/PPT
    • Programs
    • Papers
    • MCQ Quiz
    • Project/Practical File
    • Online Class-XI
  • Python-Class XII
    • Videos
      • File Handling
      • Python Functions
      • Stack
      • MySQL with Python
      • Videos-Networking
    • PDF/PPT
      • Function
      • Python Modules
      • File Handling
      • Stack and Queue
      • MySQL
      • Networking
    • Programs
      • Function Programs
      • File Handling
      • Stack and Queue
      • MySQL
      • MySQL Connectivity
    • Papers
      • Sample Papers
      • School Papers
      • Assignment/Test
    • MCQ
      • MCQ Quiz
      • MCQ Chapter-wise
    • Projects & Practical Files
    • Board Practical Exam
  • Python-Quick Revision
    • Notes
    • Errors and Outputs
    • Exam Revision Notes
    • Question Bank
  • C++
    • Notes
    • Programs/Questions
    • Project/Practical File
  • MySQL Software
  • Syllabus
  • Toggle search form

LIST

Posted on By Pallavi S No Comments on LIST

LIST

 An ordered collection of data items / elements
 Built in data structure in which individual element can be accessed using its position i.e. index value
 Represented using [] and elements are separated by comma
 Mutable data structure
 Index in the list starts from 0 for first element i.e. forward indexing
 Python allows backward indexing also. So the last element of list have index value as -1


Creating list

 Using assignment operator
1. L1 = [1,2]
2. L2 = L1 (creates deep copy)
 Using function / method
1. list()
2.copy() (creates shallow copy)
 Using eval() function
 Using [] to create blank list
 L = [45,32,’xyz’,[1,2,3],65]. Nested list
 L[i] will access i+1th element from the list- also known as indexing. For nested list, two level index will be used L[i][j]
 L[3] = ‘hello’ is permitted
 L[-2] will give [1,2,3]
 Slicing can be used to obtain a sub-part of the list. Colon (:) operator is used for
this.

 L[start: stop: step]
 Stop index value is not included.

Example

>> a=[10,20,30,40,50,60]
>>> a[2:4]
[30, 40]
>>> a[:4]
[10, 20, 30, 40]
>>> a[2:]
[30, 40, 50, 60]
>>> a[-6:-1]
[10, 20, 30, 40, 50]


 len() -gives length of the list
>>>list1=[24,”Occulent”,56.8, ‘D’]
>>>len(list1)
4

 del -can be used to delete an element or entire list
 max(), min()- functions can be used to find largest and smallest element in list. Both require name of list as argument
>>>L1=[24,56,8,90]
>>>max(L1)
90
>>>min(L1)
8

 Traversing a list , -l
for i in l:
print i

Traverses a list by it’s element. List element can’t be updated using i.

for i in range(len(l)):
print l[i]


List methods / functions

 Methods for adding elements
 insert(indexValue,element)
 append(element)
 extend(listOfElements)

 Methods for removing elements

1. remove(element)
2. pop(index)-Default index value is last element of list also returns the deleted element.
3. del statement can also be used. It can delete a slice of list as well
4. clear()- Removes all elements of the list

 reverse() method-Reverses order of elements in the same list
 index() method– Returns index of the element passed as argument. In case there is more than one value, then index of first appearance will be returned.Error in case element does not exist.

 count() method-Returns number of occurrences of the element passed as argument to the method

 sort() method-Accepts list as argument and make changes in the same list

 sorted() function– Accepts a list and returns a list created after sorting the original one.

 + operator – is used for combining two lists. It returns a new list.

 * operator – is used for creating a new list by repeating the elements by give number of times. Require a list and an integer to work on.

 A = [1,2,3]*2
 Values in A will be [1,2,3,1,2,3]

 Using list() with string returns a list of characters of the string

 in operator checks for existence of an element in the list Function Purpose Example
list.append(object)
where object can be a number, string or another list, dictionary etc.

The append function adds the object at the end of the existing list as a single entity.

>>>x=[1,2,3,4]
>>>x.append(7)
>>>print(x)
[1, 2, 3, 4, 7]
>>> a=[‘cat’,’mat’,’rat’]
>>> a.append(‘bat’)
>>> a
[‘cat’, ‘mat’, ‘rat’, ‘bat’]

>> a=[1,2,3]
>>> a.append([6,7,8])
>>> a
[1, 2, 3, [6,7,8]]


list.extend (iterable) where iterable can be a list, dictionary etc. It adds (appends) the iterable to the list

>> m=[1,2,3]
>>> m.extend([5,6,7])
>>> m
[1, 2, 3, 5, 6, 7]


list.insert(index, value)-It inserts a given value at the index position mentioned.

>>> a=[2,4,7]
>>> a.insert(2,90) #insert 90 at #index 2
>>> a
[2, 4, 90, 7]
>>> a.insert(1,”Good”)
>>> a
[2, ‘Good’, 4, 90, 7]
>>> a.insert(3,[2,3,4])
>>> a
[2, ‘Good’, 4, [2, 3, 4], 90, 7]


list.reverse()- This function simply reverses a list, by changing the order of the elements in a list.

>> a=[22,44,56,11,99, 88]
>>> a.reverse()
>>> a
[88, 99, 11, 56, 44, 22]


list.index(item)- This function returns the index value of the first occurrence of an item in
the list. If there are more than one occurrences of an item , the index value of the first occurrence only
will be returned.

>>>
city=[“Delhi”,”Mumbai”,”Chennai”,”Kolkatta”]
>>> city.index(“Mumbai”)
1

>>> list=[2,3,4,2,5,6]
>>> list.index(2)
0


list.sort()- This function sorts a list in an ascending order by default.

>> a=[22,44,56,11,99, 88]
>>>a.sort()
>>> a
[11, 22, 44, 56, 88, 99]

 


list.count(element)- This function returns the number of occurrences of a particular element in the list.

>> y=[22,66,77,44,66,88,99,22]
>>> y.count(22)
2
>>> y.count(99)
1
>>> y.count(38)
0


list.remove(object) Removes an object specified in the brackets, from the list. If we try to use the remove() an object

>>> x=[2,4,5,2,7,9,8,9]
>>>x.remove(8)
>>> x
[2, 4, 5, 2, 7, 9, 9]

which does not exist, an error occurs.

>>>
Fruits=[“Apple”,”Mango”,”Guava”,”Orange”]
>>> print(Fruits)
[‘Apple’, ‘Mango’, ‘Guava’, ‘Orange’]
>>> Fruits.remove(“Guava”)
>>> Fruits
[‘Apple’, ‘Mango’, ‘Orange’]


list.pop(index) -The method removes an item whose index value is mentioned from the list.
If we use the pop function without the index value, the last element in the list will be removed.
We can use negative indexing with the pop() function.
The pop() function when used with out of range index will give an error.

>>> Fruits.pop(3)
‘Papaya’
>>>Fruits
[‘Apple’, ‘Mango’, ‘Orange’]
>>> Fruits.pop()
‘Orange’
>>> Fruits
[‘Apple’, ‘Mango’]

>> x=[2,4,6,8,9,13]
>>> x.pop(-1)
13


list.clear() -This function simply removes all elements from the list.

>>> y=[22,66,77,44,66,88,99,22]
>>>y.clear()
>>> y
[]


del list(index)
del list-It deletes an element of the list specified by the index value. It can be used to delete the entire list also.

>>> l=[13,78,90,24]
>>> del l[1]
>>> l
[13, 90, 24]


 

 

 

 

Class XI, Class XI

Post navigation

Previous Post: Dictionary
Next Post: MySQL-Constraints

Leave a Reply Cancel reply

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

Recent Posts

  • Tips-Networking Case-based Questions
  • XII-Board Practical Exam 2020-21
  • Stack and Queue
  • Questions on Tuples
  • Networking -Full Forms

Recent Comments

    Categories

    • About
    • C++
      • Notes
      • Programs/Questions
      • Project/Practical File
    • Class XI
      • MCQ
      • Online Class-XI
      • PDF/PPT
      • Project/Practical File
    • Class XII
      • Assignment/Test
      • Board Practical Exam
      • File Handling
      • File Handling
      • Function Programs
      • Notes
      • Projects and Practical Files
        • Stack
          • Stack and Queue
      • Quick Revision
        • Errors and Outputs
        • Exam Revision Notes
        • Question Bank
      • Stack and Queue
    • File Handling
    • Function
    • Home
    • MCQ Chapter-wise
    • MCQ Quiz
    • MCQ Quiz
    • MySQL
    • MySQL Software
    • Papers
    • Papers
    • PDF/PPT
      • MySQL
      • MySQL with Python
      • Networking
    • Practical/Project
    • Programs
    • Programs
      • MySQL Connectivity
    • Python Functions
    • Python Modules
    • Python_fundamentals
    • Sample Papers
    • School Papers
    • Syllabus
    • Videos
    • Videos
    • Videos-Networking

    Archives

    • March 2021
    • February 2021

    Categories

    • Assignment/Test paper
    • Board Practical Exam
    • Class XI
    • Class XI
    • Class XII
    • Function
    • MySQL Notes
    • MySQL with Python
    • Networking
    • tuple
    • XI Videos

    Copyright © 2025 cs2study.

    Powered by PressBook WordPress theme