Computer Science: Class 11 — Chapter 9: Lists

9.1 Introduction to List

A list is an ordered sequence of elements that is mutable (changeable). Unlike strings, which only contain characters, lists can contain elements of mixed data types, including integers, floats, strings, tuples, or even other lists.

9.1.1 Accessing Elements in a List

Elements are accessed using zero-based indexing for forward traversal, and negative indexing for backward traversal.

  
    >>> list1 = [2, 4, 6, 8, 10, 12]
    >>> list1[0]
    2
    >>> list1[-1]
    12
  
9.1.2 Lists are Mutable

Lists can be modified in-place after creation. Assigning a new value to an index updates the list directly without creating a new list object.

  
    >>> list1 = ['Red', 'Green', 'Blue']
    >>> list1[2] = 'Black'
    >>> list1
    ['Red', 'Green', 'Black']
  
9.2 List Operations
9.2.1 Concatenation (+)

Joins two or more lists together. Both operands must be of type list , otherwise a TypeError is raised.

  
    >>> [1, 2] + [3, 4]
    [1, 2, 3, 4]
  
9.2.2 Repetition (*)

Replicates a list a specified number of \times.

  
    >>> ['Hello'] * 3
    ['Hello', 'Hello', 'Hello']
  
9.2.3 Membership (in, not in)

Checks if an element exists within the list, returning a boolean value ( True or False ).

  
    >>> 'Green' in ['Red', 'Green', 'Blue']
    True
  
9.2.4 Slicing

Extracts a subset of elements using the syntax list[start:stop:step] .

  • Positive Step: Slices from left to right.
  • Negative Step: Slices from right to left (reverses the list if step is -1).
  
    >>> list1 = ['Red', 'Green', 'Blue', 'Cyan', 'Magenta']
    >>> list1[1:4]
    ['Green', 'Blue', 'Cyan']
    >>> list1[::-1]
    ['Magenta', 'Cyan', 'Blue', 'Green', 'Red']
  
9.3 Traversing a List

Traversing refers to accessing each element of the list systematically. This can be achieved using loops.

Method A: Using a for Loop

Direct element iteration:

  
    for item in list1:
    print(item)
  

Index-based iteration using range() and len() :

  
    for i in range(len(list1)):
    print(list1[i])
  
Method B: Using a while Loop
  
    i = 0
    while i < len(list1):
    print(list1[i])
    i += 1
  
9.4 List Methods and Built-in Functions

Python provides a rich set of built-in functions and methods for list manipulation.

Method / Function Description Example
len() Returns the number of elements in the list. len([10, 20, 30]) → 3
list() Creates an empty list or converts a sequence to a list. list('abc') → ['a', 'b', 'c']
append() Adds a single element to the end of the list. l.append(40)
extend() Appends all elements of another list to the end. l.extend([50, 60])
insert() Inserts an element at a specified index. l.insert(1, 15)
count() Returns the frequency of an element in the list. l.count(10)
index() Returns the index of the first occurrence of an element. Raises ValueError if not found. l.index(20)
remove() Removes the first occurrence of a specified element. Raises ValueError if not found. l.remove(30)
pop() Removes and returns the element at the given index (default is the last element). l.pop(2)
reverse() Reverses the elements of the list in-place. l.reverse()
sort() Sorts the elements of the list in-place. l.sort()
sorted() Returns a new sorted list, leaving the original list unchanged. sorted(l)
min() , max() , sum() Returns the minimum, maximum, or sum of elements in the list. sum([1, 2, 3]) → 6

Technical Profile: append() vs extend()

  • Architecture Layer / Tier: Built-in Python List Methods
  • Operational Mechanics & Syntax: append(element) adds the argument as a single element (even if it is a list). extend(sequence) iterates over the sequence and appends each item individually.
  • System Trade-offs & Failures: Using append() with a list argument creates a nested list structure, whereas extend() maintains a flat list structure.

Technical Profile: sort() vs sorted()

  • Architecture Layer / Tier: In-place modification vs. Pure functional transformation
  • Operational Mechanics & Syntax: list.sort() modifies the original list in-place and returns None . sorted(list) is a built-in function that returns a new sorted list object.
  • System Trade-offs & Failures: sort() is more memory-efficient as it does not create a copy, but it destroys the original order of the list.
9.5 Nested Lists

A nested list is a list that appears as an element inside another list.

To access elements within a nested list, use double indexing: list1[i][j] , where i is the index of the nested list, and j is the index of the element inside that nested list.

  
    >>> list1 = [1, 2, ['a', 'b', 'c'], 4]
    >>> list1[2][1]
    'b'
  
9.6 Copying Lists

Assigning a list to another variable (e.g., list2 = list1 ) does not create a copy. Instead, it creates an alias . Both variables point to the same object in memory, meaning changes to one will affect the other.

To create a true copy (shallow copy) of a list, use one of the following three methods:

  1. Slicing: newList = oldList[:]
  2. Built-in list() function: newList = list(oldList)
  3. copy() function from the copy module:
      
        import copy
        newList = copy.copy(oldList)
      
    
9.7 List as Argument to a Function

When a list is passed as an argument to a function, Python passes a reference to the list. This leads to two distinct behaviors:

Case Study: Program 9-1 vs Program 9-2 (Parameter Passing Behaviors)


Socio-Technical Analysis & Core Systemic Impact: In Program 9-1, the function increment(list2) modifies elements in-place using list2[i] += 5 . Because lists are mutable and passed by reference, the ID of the list remains identical inside and outside the function, and the caller's list is updated. In Program 9-2, the statement list2 = [15, 25, 35, 45, 55] reassigns the local variable to a new list object, changing its memory ID. Consequently, any subsequent modifications only affect the local copy, leaving the original list in the main scope untouched.

9.8 Technical Concept Characterization Matrix
Core Computer Science Concept Technical Definition / Operational State Structural Parameters & Specifications Computational Function & System Purpose
List Mutability The ability to modify list elements in-place without changing the list's memory address. Modifiable via index assignment (e.g., L[i] = val ) or in-place methods like append() . Enables dynamic, memory-efficient data structures that can grow, shrink, or update during execution.
List Aliasing Multiple reference variables pointing to the exact same list object in memory. Created via simple assignment: list2 = list1 . Both share the same id() . Saves memory by avoiding redundant copies, but introduces side effects where modifications affect all aliases.
Shallow Copying Creating a new list object containing references to the elements of the original list. Achieved via list1[:] , list(list1) , or copy.copy(list1) . Isolates the outer list structure from modifications made to the copy, preventing unintended side effects.