Working with Chapter Lists and Dictionaries
Working with Chapter Lists and Dictionaries — Study Notes
NCERT-aligned · 14 notes · 3 shown free
4.1 INTRODUCTION TO LIST
Explanation4.1 INTRODUCTION TO LIST
In Python programming, a list is a fundamental data type that represents an ordered sequence of elements. Unlike strings, which are sequences of characters, lists can contain elements of different data types such as integers, floats, strings, tuples, or even other lists, making them versatile for grouping mixed data. Lists are mutable, meaning their contents can be changed after creation. Elements in a list are enclosed within square brackets [ ] and separated by commas. This allows easy grouping and manipulation of data items. For example, a list of even numbers can be represented as [2, 4, 6, 8, 10, 12], and a list of vowels as ['a', 'e', 'i', 'o', 'u']. Lists can also be nested, meaning a list can contain other lists as elements, such as [['Physics', 101], ['Chemistry', 202], ['Mathematics', 303]]. This feature is useful for representing complex data structures. Lists maintain the order of elements, which means the sequence in which elements are added is preserved. This property is essential when the order of data matters. Lists are widely used in programming for tasks such as storing collections of items, iterating over data, and performing various operations like adding, removing, or modifying elements.
- List is an ordered sequence of elements enclosed in square brackets.
- Lists can contain elements of different data types including other lists (nested lists).
- Lists are mutable, allowing modification after creation.
- Elements in a list are separated by commas.
- Lists preserve the order of elements as inserted.
- Useful for grouping heterogeneous data.
- 📌 List: An ordered, mutable sequence of elements enclosed in square brackets.
- 📌 Mutable: Ability to change the contents after creation.
- 📌 Nested List: A list containing other lists as elements.
4.1.1 Accessing Elements in a List
Explanation4.1.1 Accessing Elements in a List
Accessing elements in a list is done using indexing, where each element is assigned a position number starting from 0 for the first element. This zero-based indexing means the first element is at index 0, the second at index 1, and so forth. To access an element, the list name is followed by square brackets containing the index value. For example, list1[0] returns the first element of list1. Python also supports negative indexing, where -1 refers to the last element, -2 to the second last, and so on, allowing easy access from the end of the list. If an index outside the valid range is used, Python raises an IndexError. The length of a list can be found using the len() function, which returns the total number of elements. Using the length, the last element can be accessed by list1[len(list1) - 1] or equivalently list1[-1]. This indexing mechanism is fundamental for list traversal and manipulation.
- List indexing starts at 0 for the first element.
- Negative indexing accesses elements from the end (-1 is last element).
- Elements are accessed using list_name[index].
- Using an out-of-range index raises IndexError.
- len(list) returns the number of elements in the list.
- Indexing supports expressions resulting in integer indices.
- 📌 Indexing: Accessing elements by their position number starting at 0.
- 📌 Negative Indexing: Accessing elements from the end using negative numbers.
- 📌 len(): A built-in function that returns the length of a list.
4.1.2 Lists are Mutable
Concept4.1.2 Lists are Mutable
Lists in Python are mutable, meaning their contents can be changed after the list has been created. This mutability allows modification of individual elements by assigning new values to specific indices. For example, if a list contains color names, c
Practice Questions — Working with Chapter Lists and Dictionaries
Includes NCERT exercise questions with answers
Q1.1. What will be the output of the following statements? a) list1 = [12,32,65,26,80,10] list1.sort() print(list1) b) list1 = [12,32,65,26,80,10] sorted(list1) print(list1) c) list1 = [1,2,3,4,5,6,7,8,9,10] list1[::-2] list1[:3] + list1[3:] d) list1 = [1,2,3,4,5] list1[len(list1)-1]
Answer:
a) list1.sort() sorts the list in place. So after sorting, list1 becomes [10, 12, 26, 32, 65, 80]. The print statement outputs: [10, 12, 26, 32, 65, 80] b) sorted(list1) returns a new sorted list but does not modify list1 itself. Since the returned sorted list is not assigned, list1 remains unchanged. So print(list1) outputs: [12, 32, 65, 26, 80, 10] c) list1[::-2] returns a slice starting from the end towards the start, stepping by 2. So it picks elements at indices 9,7,5,3,1 (0-based): [10,8,6,4,2]. However, this expression is not printed or assigned, so no output. list1[:3] + list1[3:] returns the entire list (first 3 elements plus from 3rd index onwards), effectively the same list. Again, no print or assignment, so no output. d) list1[len(list1)-1] accesses the last element of list1. For list1 = [1,2,3,4,5], len(list1) = 5, so index 4 is last element which is 5. However, no print statement, so no output. Summary of outputs: a) [10, 12, 26, 32, 65, 80] b) [12, 32, 65, 26, 80, 10] c) No output d) No output
Explanation:
a) The sort() method sorts the list in place, modifying the original list. b) The sorted() function returns a new sorted list but does not change the original list unless assigned. c) Slicing operations without print or assignment produce no output. d) Accessing an element without print produces no output.
Q2.2. Consider the following list myList. What will be the elements of myList after each of the following operations? myList = [10,20,30,40] a) myList.append([50,60]) b) myList.extend([80,90])
Answer:
Initial list: myList = [10, 20, 30, 40] a) myList.append([50,60]) adds the entire list [50,60] as a single element at the end. So myList becomes: [10, 20, 30, 40, [50, 60]] b) myList.extend([80,90]) adds each element of the list [80,90] individually to the end of myList. So after extending, myList becomes: [10, 20, 30, 40, [50, 60], 80, 90]
Explanation:
append() adds its argument as a single element at the end of the list. extend() iterates over its argument and adds each element individually to the list.
Q3.3. What will be the output of the following code segment? myList = [1,2,3,4,5,6,7,8,9,10] for i in range(0,len(myList)): if i%2 == 0: print(myList[i])
Answer:
The code iterates over indices 0 to 9 of myList. For each index i, if i is even (i%2 == 0), it prints myList[i]. Indices 0,2,4,6,8 are even. Corresponding elements are: 1, 3, 5, 7, 9. Hence, output will be: 1 3 5 7 9
Explanation:
The for loop runs from 0 to length of list - 1. The if condition filters even indices. Elements at even indices are printed.
Q4.4. What will be the output of the following code segment? a) myList = [1,2,3,4,5,6,7,8,9,10] del myList[3:] print(myList) b) myList = [1,2,3,4,5,6,7,8,9,10] del myList[:5] print(myList) c) myList = [1,2,3,4,5,6,7,8,9,10] del myList[::2] print(myList)
Answer:
a) del myList[3:] deletes elements from index 3 to end. Original list: [1,2,3,4,5,6,7,8,9,10] After deletion: [1,2,3] Output: [1, 2, 3] b) del myList[:5] deletes elements from start to index 4. Original list: [1,2,3,4,5,6,7,8,9,10] After deletion: [6,7,8,9,10] Output: [6, 7, 8, 9, 10] c) del myList[::2] deletes elements at indices 0,2,4,6,8. Original list: [1,2,3,4,5,6,7,8,9,10] Elements at indices 0,2,4,6,8 are 1,3,5,7,9. After deletion, remaining elements are at indices 1,3,5,7,9: 2,4,6,8,10 Output: [2, 4, 6, 8, 10]
Explanation:
del with slice removes specified elements from the list. In (a), elements from index 3 onwards are removed. In (b), elements from start to index 4 are removed. In (c), elements at every second index starting from 0 are removed.
Q5.5. Differentiate between append() and extend() methods of list.
Answer:
append(): Adds its argument as a single element to the end of the list. Example: list1.append([50,60]) adds the list [50,60] as one element. extend(): Iterates over its argument and adds each element individually to the list. Example: list1.extend([50,60]) adds 50 and 60 as separate elements. Summary: - append() increases list length by 1. - extend() increases list length by number of elements in the argument.
Explanation:
append() treats the argument as a single element. extend() treats the argument as an iterable and adds each element separately.
Q6.6. Consider a list: list1 = [6,7,8,9] What is the difference between the following operations on list1: a) list1 * 2 b) list1 *= 2 c) list1 = list1 * 2
Answer:
Given list1 = [6,7,8,9] a) list1 * 2 returns a new list which is list1 repeated twice: [6,7,8,9,6,7,8,9] But list1 itself remains unchanged. b) list1 *= 2 modifies list1 in place by repeating its contents twice. After this operation, list1 becomes: [6,7,8,9,6,7,8,9] c) list1 = list1 * 2 creates a new list by repeating list1 twice and assigns it back to list1. Effectively same as (b), list1 becomes: [6,7,8,9,6,7,8,9] Summary: - a) does not change list1, just returns a new list. - b) modifies list1 in place. - c) creates a new list and reassigns list1 to it.
Explanation:
The * operator on lists repeats the list. The *= operator modifies the list in place. Assignment with = replaces the reference with a new list.
Q7.7. The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the following list: stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8] Write Python statements to retrieve the following information from the list stRecord. - a) Percentage of the student - b) Marks in the fifth subject - c) Maximum marks of the student - d) Roll No. of the student - e) Change the name of the student from 'Raman' to 'Raghav'
Answer:
Given: stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8] Solutions: a) Percentage of the student: print(stRecord[3]) # Output: 78.8 b) Marks in the fifth subject: print(stRecord[2][4]) # Output: 69 c) Maximum marks of the student: print(max(stRecord[2])) # Output: 99 d) Roll No. of the student: print(stRecord[1]) # Output: 'A-36' e) Change the name from 'Raman' to 'Raghav': stRecord[0] = 'Raghav' print(stRecord[0]) # Output: 'Raghav'
Explanation:
List indexing: - stRecord[3] accesses percentage. - stRecord[2] is list of marks; stRecord[2][4] accesses fifth subject marks. - max() function finds maximum in marks list. - stRecord[1] is roll number. - Assignment changes name at index 0.
Q8.8. Consider the following dictionary stateCapital: stateCapital = {"Assam":"Guwahati", "Bihar":"Patna","Maharashtra":"Mumbai", "Rajasthan":"Jaipur"} Find the output of the following statements: - a) print(stateCapital.get("Bihar")) - b) print(stateCapital.keys()) - c) print(stateCapital.values()) - d) print(stateCapital.items()) - e) print(len(stateCapital)) - f) print("Maharashtra" in stateCapital) - g) print(stateCapital.get("Assam")) - h) del stateCapital["Assam"] print(stateCapital)
Answer:
Given: stateCapital = {"Assam":"Guwahati", "Bihar":"Patna", "Maharashtra":"Mumbai", "Rajasthan":"Jaipur"} Outputs: a) print(stateCapital.get("Bihar")) Output: Patna b) print(stateCapital.keys()) Output: dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan']) c) print(stateCapital.values()) Output: dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur']) d) print(stateCapital.items()) Output: dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')]) e) print(len(stateCapital)) Output: 4 f) print("Maharashtra" in stateCapital) Output: True g) print(stateCapital.get("Assam")) Output: Guwahati h) del stateCapital["Assam"] print(stateCapital) Output: {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
Explanation:
get() returns value for given key. keys() returns all keys. values() returns all values. items() returns key-value pairs. len() returns number of key-value pairs. 'in' operator checks key presence. del removes key-value pair.
All 8 Chapters in Informatics Practices
Informatics Practices · Class 11