04 Working with List and Dictionary

CBSE Class 11 Informatics Practices

4A List

A list is an ordered and mutable sequence of items.

4.1 - Creating and Initializing Lists

  • Lists hold ordered, mutable collections
  • Defined using [ ] separated by commas
  • Can contain mixed data types and nested lists

    # Creating Python lists
    numbers = [12, 25, 37, 41, 59]
    mixed_data = ["Alice", 17, 92.5, True]
    nested = [1, 2, [3, 4, 5], 6]
    
    print("Numbers:", numbers)
    print("Mixed:", mixed_data)
    print("Nested List:", nested)
    print("Length of 'numbers' =", len(numbers))
    

Python allows lists to contain values of different data types, including another list inside it. The len() function returns the number of elements.

4.2 - Accessing and Traversing Lists

  • Elements accessed via indexing [ ]
  • Index starts at 0; negative index from end
  • Traversing uses loops

    fruits = ["apple", "banana", "cherry", "mango", "grapes"]
    
    print("First fruit:", fruits[0])
    print("Last fruit:", fruits[-1])
    
    print("\nTraversing list:")
    for item in fruits:
    print("Fruit:", item)
    

Indexing retrieves elements using position. fruits[0] returns the first element and fruits[-1] returns the last. Traversing means accessing each value sequentially, commonly using a for loop.

4.3 - List Mutability (Updating Values)

  • Lists are mutable → elements can be changed
  • Individual items can be updated by assigning new values
  • Useful in data processing workflows

    marks = [78, 82, 69, 88, 91]
    print("Before update:", marks)
    
    marks[2] = 72               # modify single value
    marks[4] = marks[4] + 5     # increase last subject marks
    
    print("After update:", marks)
    

Since lists are mutable, values can be reassigned using index operations. Such operations are common in grading systems or analytics tasks.

4.4 - List Operations & Slicing

  • + (concatenation),
  • * (repetition),
  • Membership using in
  • Slicing extracts sub-lists

    a = [1, 2, 3]
    b = [4, 5]
    
    print(a + b)         # concatenation
    print(a * 3)         # repetition
    print(2 in a)        # membership test
    
    data = [10, 20, 30, 40, 50, 60]
    print(data[2:5])     # slice
    print(data[::-1])    # reverse list using slicing
    

Concatenation and repetition create new lists. The in operator checks membership. Slicing extracts a portion using indices and optional step size.

4.5 - Useful List Methods

  • Adding elements: append(), extend(), insert()
  • Removing elements: remove(), pop()
  • Sorting & reversing

    scores = [55, 72, 88, 61, 95, 78]
    
    scores.append(84)
    scores.insert(2, 90)
    scores.remove(61)
    removed = scores.pop()
    
    scores.sort(reverse=True)
    print("Updated Scores:", scores)
    print("Removed Value:", removed)
    

Methods allow structured manipulation. append() adds at end, insert() adds at specific position, and sort() reorders the list.

4B Dictionary

A dictionary represents structured information where each piece of data is associated with a key. Dictionaries are enclosed in curly braces.

4.6 - Creating & Initializing Dictionaries

  • Dictionary stores data in key: value pairs
  • Keys must be unique and immutable
  • Values can repeat and be of any type

    student = {
    "name": "Ishaan",
    "age": 16,
    "subjects": ["Math", "CS", "Physics"],
    "score": {"Math": 92, "CS": 88, "Physics": 81}
    }
    
    print(student)
    

4.7 - Accessing and Updating Dictionary Values

  • Access using keys
  • Modify values or add new pairs

    student = {"name": "Ishaan", "age": 16, "city": "Delhi"}
    
    print("Name:", student["name"])
    
    student["age"] = 17
    student["grade"] = "XI"
    
    print(student)
    

Accessing a dictionary uses the key, not a numeric index. New entries can be added dynamically.

4.8 - Traversing Dictionary Elements

  • keys(), values(), items()
  • Looping through elements using for loops.

    student = {"name": "Ishaan", "age": 17, "city": "Delhi"}
    
    for key in student.keys():
    print("Key:", key)
    
    for value in student.values():
    print("Value:", value)
    
    for key, value in student.items():
    print(key, "=>", value)
    

Dictionary traversal can be done through keys, values, or key-value pairs using items().

4.9 - Dictionary Methods

  • get(), update(), clear(), del
  • Avoid KeyError with get()

    employee = {"id": 101, "name": "Arjun", "salary": 45000}
    
    print(employee.get("salary"))      
    print(employee.get("bonus"))        # None instead of error
    
    employee.update({"department": "Finance"})
    del employee["salary"]
    
    print(employee)
    

get() safely returns a value when the key may not exist. update() adds or modifies entries. del deletes specific items.

4.10 - Real-world Example Combining Data

  • Dictionaries storing structured records
  • Extracting results and generating summaries

    students = {
    "Aarav": [88, 91, 79],
    "Diya": [92, 85, 87],
    "Kabir": [76, 83, 80]
    }
    
    for name, scores in students.items():
    avg = sum(scores) / len(scores)
    print(name, "→ Average =", round(avg, 2))
    

Dictionaries help organize real-world datasets. Here, we store student scores and compute averages using list operations inside dictionary traversal.


Key Conceptual Takeaways

  1. Python is an open-source, high level, interpreterbased language that can be used for a multitude of scientific and non-scientific computing purposes.
  2. Comments are non-executable statements in a program.
  3. An identifier is a user defined name given to a variable or a constant in a program.
  4. Process of identifying and removing errors from a computer program is called debugging.
  5. Trying to use a variable that has not been assigned a value gives an error.
  6. There are several data types in Python — integer, boolean, float, complex, string, list, tuple, sets, None and dictionary.
  7. Operators are constructs that manipulate the value of operands. Operators may be unary or binary.
  8. An expression is a combination of values, variables, and operators.
  9. Python has input() function for taking user input.
  10. Python has print() function to output data to a standard output device.
  11. The if statement is used for decision making.
  12. Looping allows sections of code to be executed repeatedly under some condition.
  13. for statement can be used to iterate over a range of values or a sequence.
  14. The statements within the body of for loop are executed till the range of values is exhausted.
Page last updated: 19-Jan-2026
Last Change: "some changes for timezone and git checkin history pull by hugo" (#b34f1bc)