Computer Science · Class 11 Optional
Ch 9Lists — Class 11 Computer Science, concept-first.
A list is Python's workhorse container: an ordered sequence of values that is mutable, meaning its contents can be changed after it is created. A list is made up of one or more elements, and — unlike a string, which can hold only characters — a list may freely mix elements of different data types: integers, floats, str…
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
List Data Type
Imagine you're organising a small party. You have a guest list — a single piece of paper where you write down names, one after another, in a specific order.
Most relevant Q&A
- Example 9.1: Create lists of different kinds and print each — list1, a list of six even numbers; list2, a list of vowels; list3, a list of m…Preview
- Example 9.2: Given the nested list `list1 = [1,2,'a','c',[6,7,8],4,9]`, access the inner list with `list1[4]` and access an element of the n…Preview
- Example 9.3: Copy a list using slicing (Method 1, `newList = oldList[:]`) — create a copy of `list1 = [1,2,3,4,5]` with `list2 = list1[:]`.Preview
- Example 9.4: Copy a list using the built-in function list() (Method 2, `newList = list(oldList)`) — create a copy of `list1 = [10,20,30,40]`…Preview
- Example 9.5: Copy a list using the copy() function of the python library copy (Method 3, `newList = copy.copy(oldList)`) — import copy and c…Preview
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction to List
A list is Python's workhorse container: an ordered sequence of values that is mutable, meaning its contents can be changed after it is created.
Accessing Elements in a List
Individual elements of a list are picked out exactly the way characters are picked out of a string: write the list's name followed by the element's index in square brackets.
Lists are Mutable
Lists in Python are mutable: the contents of a list can be changed after the list has been created. This is the single most important property separating a list from a string — a string, once built, c…
List Operations
The list data type lets you manipulate its contents through a family of operations, each covered in its own sub-section:
Concatenation
Python joins two or more lists using the concatenation operator, written with the symbol +. The result is a single new list containing the elements of the first list followed by the elements of the se…
Repetition
Python can replicate a list using the repetition operator, written with the symbol . Multiplying a list by an integer n produces a new list in which the original elements are repeated n times over, in…
Membership
Just as with strings, Python provides membership operators to ask whether a value occurs in a list. There are two of them, and each returns a Boolean result — True or False.
Slicing
The slicing operation that extracts substrings from a string applies to lists as well: list[start:stop] returns a new list containing the elements from index start up to but not including index stop.
Traversing a List
Traversing a list means visiting each of its elements, one after another — usually to print, examine or process them. Python offers two loop constructs for this: the for loop and the while loop.
List Methods and Built-in Functions
The list data type comes with a rich set of built-in methods and functions for everyday programming: measuring a list, building one, adding and removing elements, searching, rearranging, and summarisi…
Nested Lists
When a list appears as an element of another list, the inner list is called a nested list. Nesting lets one list carry structured sub-groups of data alongside ordinary elements.
Copying Lists
3 QCopying a list is one of the places where Python's behaviour surprises beginners: the "obvious" way — plain assignment — does not create a copy at all.
+−Worked Examples1 question
+−Worked Examples1 question
List as Argument to a Function
2 QWhen a list is passed as an argument to a function, what we actually pass is a reference to the list — not a fresh copy of it.
List Manipulation
3 QThis closing section pulls the whole chapter together: creating lists, and applying the operations and methods learnt so far, inside complete working programs.
+−Programs1 question
+−Programs1 question
Summary
- Lists are mutable sequences in Python — the elements of a list can be changed after the list is created.
More questions
16 Q+−Show 7 questionsHide questions7 questions
- Q1What will be the output of the following statements? i. ```python list1 = [12,32,65,26,80,10] list1.sort() print(list1) ``` ii. ```python li…Free
- Q2Consider the following list myList. What will be the elements of myList after the following two operations: ```python myList = [10,20,30,40]…Free
- Q3What will be the output of the following code segment: ```python myList = [1,2,3,4,5,6,7,8,9,10] for i in range(0,len(myList)): if i%2 == 0:…Free
- Q4What will be the output of the following code segment: a. ```python myList = [1,2,3,4,5,6,7,8,9,10] del myList[3:] print(myList) ``` b. ```p…Preview
- Q5Differentiate between append() and extend() functions of list.Preview
- Q6Consider a list: ```python list1 = [6,7,8,9] ``` What is the difference between the following operations on list1: a. `list1 * 2` b. `list1…Preview
- Q7The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list: ```python stRecord…Preview
+−Show 9 questionsHide questions9 questions
- Q1Write a program to find the number of times an element occurs in the list.Free
- Q2Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the o…Free
- Q3Write a function that returns the largest element of the list passed as parameter.Free
- Q4Write a function to return the second largest number from a list of numbers.Preview
- Q5Write a program to read a list of n integers and find their median. Note: The median value of a list of values is the middle one when they a…Preview
- Q6Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurrin…Preview
- Q7Write a program to read a list of elements. Input an element from the user that has to be inserted in the list. Also input the position at w…Preview
- Q8Write a program to read elements of a list. a) The program should ask for the position of the element to be deleted from the list. Write a f…Preview
- Q9Read a list of n elements. Pass this list to a function which reverses this list in-place without creating a new list.Preview