Working with Lists and Dictionaries

NCERT Class 11 Informatics Practices Chapter 4: Working with Lists and Dictionaries (Pages 55–80)

Summary of Working with Lists and Dictionaries

Playing 00:00 / 00:00

Working with Lists and Dictionaries Summary

In this chapter, students will learn about lists and dictionaries, fundamental data structures in Python. First, we introduce lists, which are ordered, mutable sequences that can hold multiple types of elements, enabling easy organization and manipulation of data. For instance, a list can include integers, strings, and even other lists, allowing for diverse applications. Students will learn how to access elements using indexing, noting that the first element has an index of zero, and how to work with negative indices to access elements from the end of the list. The chapter will then cover various list operations like appending, inserting, and deleting elements, as well as methods for merging lists and repeating their contents. Slicing is introduced, enabling students to create sublists effortlessly, an essential skill for managing large datasets. This section on lists not only involves using built-in functions for lists, such as len for determining length, but also methods like sort and reverse for organizing data effectively. Next, the chapter transitions to dictionaries, emphasizing their role in storing key-value pairs, which provide a clear and efficient way to handle data relationships. It explains how to create dictionaries using curly braces and unique keys to access values quickly, fostering a deeper understanding of data mapping in programming. Methods like get, update, and delete are introduced, giving students the practical tools they need to manipulate dictionary data. Students will also engage in practical exercises and programs aimed at reinforcing what they’ve learned about lists and dictionaries. By the end of this chapter, students should be equipped to apply these concepts to solve real-world problems efficiently, highlighting the versatility and necessity of lists and dictionaries in programming.

Working with Lists and Dictionaries learning objectives

  • In this chapter, students will learn about lists and dictionaries, fundamental data structures in Python.
  • First, we introduce lists, which are ordered, mutable sequences that can hold multiple types of elements, enabling easy organization and manipulation of data.
  • For instance, a list can include integers, strings, and even other lists, allowing for diverse applications.
  • Students will learn how to access elements using indexing, noting that the first element has an index of zero, and how to work with negative indices to access elements from the end of the list.

Working with Lists and Dictionaries key concepts

  • In Chapter 4, 'Working with Lists and Dictionaries', students learn about the data type 'list', characterized by its ordered and mutable properties.
  • Lists can contain mixed data types and facilitate data grouping.
  • The chapter covers how to access list elements using indices, perform basic operations like concatenation, repetition, and membership testing.
  • Additionally, it introduces list methods for manipulation, including appending, inserting, and sorting elements.
  • Furthermore, students explore dictionaries, a key-value mapping data type that is also mutable, allowing for dynamic data management.

Important topics in Working with Lists and Dictionaries

  1. 1.Chapter 4 of Informatics Practices focuses on 'Working with Lists and Dictionaries'.
  2. 2.It introduces lists as mutable data types, explores list operations, methods, and built-in functions, and delves into dictionaries as a mapping data type.
  3. 3.This chapter is essential for mastering data organization in programming.
  4. 4.In this chapter, students will learn about lists and dictionaries, fundamental data structures in Python.
  5. 5.First, we introduce lists, which are ordered, mutable sequences that can hold multiple types of elements, enabling easy organization and manipulation of data.
  6. 6.For instance, a list can include integers, strings, and even other lists, allowing for diverse applications.

Working with Lists and Dictionaries syllabus breakdown

In Chapter 4, 'Working with Lists and Dictionaries', students learn about the data type 'list', characterized by its ordered and mutable properties. Lists can contain mixed data types and facilitate data grouping. The chapter covers how to access list elements using indices, perform basic operations like concatenation, repetition, and membership testing. Additionally, it introduces list methods for manipulation, including appending, inserting, and sorting elements. Furthermore, students explore dictionaries, a key-value mapping data type that is also mutable, allowing for dynamic data management. Methods for creating, accessing, and modifying dictionaries are covered extensively to enhance programming skills in data handling.

Working with Lists and Dictionaries Revision Guide

Revise the most important ideas from Working with Lists and Dictionaries.

Key Points

1

Lists are mutable sequences.

In Python, lists can be modified after creation, allowing changes in contents as needed.

2

Access list elements using indices.

Elements can be accessed using indices starting from 0. Negative indices refer to elements from the end.

3

List concatenation with '+' operator.

Combine lists using the '+' operator; original lists remain unchanged.

4

List repetition with '*' operator.

Repeat the contents of a list using the '*' operator; duplicates are created.

5

Membership operator 'in'.

Check if an element exists in a list using 'in', returning True or False.

6

List slicing creates sublists.

Use slicing notation to create sublists by specifying start and end indices.

7

Common list methods: append, remove.

Use 'append()' to add elements, and 'remove()' to delete specific values from a list.

8

Traverse lists using loops.

Iterate through a list with 'for' or 'while' loops, accessing and manipulating elements.

9

Dictionaries are key-value pairs.

Dictionaries store data as pairs where keys are unique and map to their corresponding values.

10

Access dictionary values with keys.

Retrieve values by referencing their unique keys; use 'get()' to avoid errors on missing keys.

11

Dictionary methods: keys(), values().

Use 'keys()' to get a list of keys, and 'values()' to retrieve all values in the dictionary.

12

Dictionaries are mutable.

We can change contents by adding new key-value pairs or updating existing ones in dictionaries.

13

Check existence in dictionaries.

Use 'in' to test if a key exists; 'not in' checks for the absence of a key.

14

Length of lists and dictionaries.

Use 'len()' to find the number of items in lists or key-value pairs in dictionaries.

15

Sorting lists with 'sort()' and 'sorted()'.

'sort()' modifies the original list, while 'sorted()' returns a new sorted list.

16

Error handling in lists.

Accessing out of range indices leads to IndexError; handle exceptions for robust code.

17

Pop and delete methods in lists.

'pop()' removes an element at a specified index; 'del' can remove elements or entire lists.

18

Dictionary update with 'update()'.

Merge another dictionary into the current one using the 'update()' method.

19

Clear dictionary with 'clear()'.

Remove all key-value pairs from a dictionary using the 'clear()' method.

20

Real-world applications of lists and dictionaries.

Used for data manipulation, storing user information, or maintaining collections of items.

Working with Lists and Dictionaries Questions & Answers

Work through important questions and exam-style prompts for Working with Lists and Dictionaries.

Show all 141 questions
Q9

If myList = [5, 10, 15] and you execute myList.append(20), what will myList look like?

Single Answer MCQ
Q-00066563
View explanation
Q10

What is the error in running this code: print([1, 2, 3][5])?

Single Answer MCQ
Q-00066564
View explanation
Q11

What would be the result of executing: list1 = [1, 2, 3], list1 += [4, 5]?

Single Answer MCQ
Q-00066565
View explanation
Q12

How would you create a list containing the first five square numbers?

Single Answer MCQ
Q-00066566
View explanation
Q13

Which of the following statements is true about Python lists?

Single Answer MCQ
Q-00066567
View explanation
Q14

If a list is defined as list1 = [1, 2, 3], which statement will create a copy of this list?

Single Answer MCQ
Q-00066568
View explanation
Q15

What is the result of executing: [1, 2, 3] + [4] * 2?

Single Answer MCQ
Q-00066569
View explanation
Q16

What is the correct syntax to create a list in Python?

Single Answer MCQ
Q-00066570
View explanation
Q17

How do you access the first element of a list named 'myList'?

Single Answer MCQ
Q-00066571
View explanation
Q18

Which of the following is a valid list in Python?

Single Answer MCQ
Q-00066572
View explanation
Q19

What will the expression list1[2] return for the list list1 = [2, 4, 6, 8]?

Single Answer MCQ
Q-00066573
View explanation
Q20

What will be the output of len(list3) if list3 = [100, 23.5, 'Hello']?

Single Answer MCQ
Q-00066574
View explanation
Q21

What is the output of list1[-1] if list1 = [10, 20, 30]?

Single Answer MCQ
Q-00066575
View explanation
Q22

What type of data can be stored in a Python list?

Single Answer MCQ
Q-00066576
View explanation
Q23

Which command would throw an 'IndexError' for the list list1 = [0, 1, 2]?

Single Answer MCQ
Q-00066577
View explanation
Q24

If list4 contains elements [['Math', 101], ['English', 102]], what does list4[0][1] return?

Single Answer MCQ
Q-00066578
View explanation
Q25

Which of the following statements will correctly append an item to a list 'myList'?

Single Answer MCQ
Q-00066579
View explanation
Q26

What does the expression list1[1:4] return for list1 = [0, 1, 2, 3, 4, 5]?

Single Answer MCQ
Q-00066580
View explanation
Q27

If list5 contains [1, 2, 3, 4], what will list5[-2] return?

Single Answer MCQ
Q-00066581
View explanation
Q28

What will be the output of the expression myList = [1, 2, 3]; myList * 2?

Single Answer MCQ
Q-00066582
View explanation
Q29

In which situation would using negative indexing be beneficial?

Single Answer MCQ
Q-00066583
View explanation
Q30

If you have the following list: myList = [2, 4, 6, 8, 10], what would be the value of myList[1:3]?

Single Answer MCQ
Q-00066584
View explanation
Q31

Given a list defined as list = [5, 3, 9, 1], which method is used to sort this list in ascending order?

Single Answer MCQ
Q-00066585
View explanation
Q32

What will be the output of the following code? list1 = [1, 2, 3, 4, 5] for item in list1: print(item)

Single Answer MCQ
Q-00066586
View explanation
Q33

Which index will return the last element of the list in Python code? list1 = [10, 20, 30, 40]

Single Answer MCQ
Q-00066587
View explanation
Q34

How can you access the third element in a list named 'my_list'?

Single Answer MCQ
Q-00066588
View explanation
Q35

What does the len() function return for list1 = [5, 10, 15]?

Single Answer MCQ
Q-00066589
View explanation
Q36

What will be the output of: list1 = [1, 2, 3] print(list1[1:3])?

Single Answer MCQ
Q-00066590
View explanation
Q37

Which of the following correctly initializes an empty list in Python?

Single Answer MCQ
Q-00066591
View explanation
Q38

How can you iterate over the elements of a list named myList using a for loop?

Single Answer MCQ
Q-00066592
View explanation
Q39

What will be the output of: list1 = [1, 2, 3] for i in range(len(list1)): print(list1[i])?

Single Answer MCQ
Q-00066593
View explanation
Q40

If list1 = [10, 20, 30] and you execute list1.append(40), what will list1 become?

Single Answer MCQ
Q-00066594
View explanation
Q41

What is the result of list1 = [1, 2, 3, 4] list1[1:3]?

Single Answer MCQ
Q-00066595
View explanation
Q42

How do you reverse the list called myList?

Single Answer MCQ
Q-00066596
View explanation
Q43

Given the list: myList = [5, 'test', 12.3], what will myList[1] return?

Single Answer MCQ
Q-00066597
View explanation
Q44

What error do you encounter when trying to access an index that exceeds the list size?

Single Answer MCQ
Q-00066598
View explanation
Q45

Which method can insert an element at a specific index of a list?

Single Answer MCQ
Q-00066599
View explanation
Q46

If you have a list of numbers, how do you access the second last number?

Single Answer MCQ
Q-00066600
View explanation
Q47

What will the output be of: print('List:', list1[::-1]) where list1 = [1, 2, 3]?

Single Answer MCQ
Q-00066601
View explanation
Q48

What will be the result of the following code? list1 = [1, 2, 3]; list1.append(4); print(list1)

Single Answer MCQ
Q-00066602
View explanation
Q49

Which function is used to determine the number of elements in a list?

Single Answer MCQ
Q-00066603
View explanation
Q50

What is the output of the code: list1 = [5, 10, 15]; list1.insert(1, 7); print(list1)?

Single Answer MCQ
Q-00066604
View explanation
Q51

Which of the following methods can be used to add elements from one list to another?

Single Answer MCQ
Q-00066605
View explanation
Q52

What will the command sorted([3, 1, 2]) return?

Single Answer MCQ
Q-00066606
View explanation
Q53

Which method would you use to remove an item at a specific index from a list?

Single Answer MCQ
Q-00066607
View explanation
Q54

If list1 = [1, 2, 3, 4, 5], what does list1[-1] return?

Single Answer MCQ
Q-00066608
View explanation
Q55

Consider list1 = [10, 20, 30, 40]; what will be the result of list1[1:3]?

Single Answer MCQ
Q-00066609
View explanation
Q56

What will the output of 'list1 = [1, 2, 2, 3]; list1.count(2)' be?

Single Answer MCQ
Q-00066610
View explanation
Q57

Given list2 = [6, 7, 8]; what does list2 * 2 produce?

Single Answer MCQ
Q-00066611
View explanation
Q58

What will the command 'myList = [3,2,1]; myList.reverse()' do?

Single Answer MCQ
Q-00066612
View explanation
Q59

If list1 = [3, 2, 4], what does max(list1) return?

Single Answer MCQ
Q-00066613
View explanation
Q60

What is the output of list1 = [1, 2, 3]; del list1[1]; print(list1)?

Single Answer MCQ
Q-00066614
View explanation
Q61

How do you create an empty list?

Single Answer MCQ
Q-00066615
View explanation
Q62

What happens if you try to access an index that is out of range?

Single Answer MCQ
Q-00066616
View explanation
Q63

Which operator is used to concatenate two lists?

Single Answer MCQ
Q-00066617
View explanation
Q64

Which method returns the number of key-value pairs in a dictionary?

Single Answer MCQ
Q-00066618
View explanation
Q65

What will dict1.keys() return for dict1 = {'A': 1, 'B': 2}?

Single Answer MCQ
Q-00066619
View explanation
Q66

What does dict1.get('key') return if 'key' does not exist in dict1?

Single Answer MCQ
Q-00066620
View explanation
Q67

Which method would you use to update a dictionary with another dictionary's values?

Single Answer MCQ
Q-00066621
View explanation
Q68

What will be the output of 'dict1 = {1: 'one', 2: 'two'}; dict1[3]'?

Single Answer MCQ
Q-00066622
View explanation
Q69

To remove all items from a dictionary, which method is used?

Single Answer MCQ
Q-00066623
View explanation
Q70

What data type does dict1.items() return?

Single Answer MCQ
Q-00066624
View explanation
Q71

What will be the result of executing dict1.update({'A': 1}) when dict1 = {'A': 2}?

Single Answer MCQ
Q-00066625
View explanation
Q72

Which of the following will correctly create a dictionary?

Single Answer MCQ
Q-00066626
View explanation
Q73

If dict1 = {'A': 1, 'B': 2} and you perform dict1.pop('B'), what will dict1 become?

Single Answer MCQ
Q-00066627
View explanation
Q74

Which method will you use to get values from a dictionary into a list format?

Single Answer MCQ
Q-00066628
View explanation
Q75

What does dict1['key'] return when 'key' is not present in dict1?

Single Answer MCQ
Q-00066629
View explanation
Q76

In a dictionary, how can you retrieve a default value if a key is not found?

Single Answer MCQ
Q-00066630
View explanation
Q77

Which method would clear all items from dict1?

Single Answer MCQ
Q-00066631
View explanation
Q78

What does dict1.popitem() return?

Single Answer MCQ
Q-00066632
View explanation
Q79

Which method converts a sequence of key-value pairs into a dictionary?

Single Answer MCQ
Q-00066633
View explanation
Q80

What is the correct syntax to create an empty dictionary in Python?

Single Answer MCQ
Q-00066634
View explanation
Q81

Which statement about dictionary keys in Python is correct?

Single Answer MCQ
Q-00066635
View explanation
Q82

Given the dictionary dict3 = {'a':1, 'b':2, 'c':3}, what is dict3['b']?

Single Answer MCQ
Q-00066636
View explanation
Q83

What will the following code output? dict3 = {'x': 1, 'y': 2}; print(dict3.get('z', 0))

Single Answer MCQ
Q-00066637
View explanation
Q84

How can you add a new key-value pair to an existing dictionary?

Single Answer MCQ
Q-00066638
View explanation
Q85

What type of data structure is a dictionary in Python?

Single Answer MCQ
Q-00066639
View explanation
Q86

In the dictionary dict2 = {'name': 'Alice', 'age': 25}, which of the following expressions accesses the name?

Single Answer MCQ
Q-00066640
View explanation
Q87

If a dictionary has duplicate keys, what happens when it is created?

Single Answer MCQ
Q-00066641
View explanation
Q88

How can you delete a key-value pair from a dictionary?

Single Answer MCQ
Q-00066642
View explanation
Q89

What will be returned when you call dict2 = {'key': 'value'}; dict2.keys()?

Single Answer MCQ
Q-00066643
View explanation
Q90

Which of the following will raise a KeyError?

Single Answer MCQ
Q-00066644
View explanation
Q91

Given a dictionary, how would you change the value associated with an existing key?

Single Answer MCQ
Q-00066645
View explanation
Q92

What is the output of the expression len({'a': 1, 'b': 2})?

Single Answer MCQ
Q-00066646
View explanation
Q93

Which of the following represents a valid dictionary with mixed value types?

Single Answer MCQ
Q-00066647
View explanation
Q94

If you want to retrieve a value and handle the case where the key might not exist, which method would you use?

Single Answer MCQ
Q-00066648
View explanation
Q95

Which of the following statements is true regarding dictionary items?

Single Answer MCQ
Q-00066649
View explanation
Q96

What is the primary way to traverse a dictionary in Python?

Single Answer MCQ
Q-00066650
View explanation
Q97

Given the dictionary `dict1 = {'a': 1, 'b': 2, 'c': 3}`, what will be printed by the following code? `for k, v in dict1.items(): print(k, v)`

Single Answer MCQ
Q-00066651
View explanation
Q98

What happens if you try to access a key that does not exist in a dictionary using the `get` method?

Single Answer MCQ
Q-00066652
View explanation
Q99

In a dictionary, how can you find the number of key-value pairs?

Single Answer MCQ
Q-00066653
View explanation
Q100

What will the statement `dict1.update({'d': 4})` do if `dict1` is originally `{'a': 1, 'b': 2}`?

Single Answer MCQ
Q-00066654
View explanation
Q101

When using the `keys()` method on a dictionary, what is returned?

Single Answer MCQ
Q-00066655
View explanation
Q102

What is the output of the following code? `dict1 = {'a': 1, 'b': 2}; print(dict1.get('c'))`

Single Answer MCQ
Q-00066656
View explanation
Q103

Which of the following will give you both the keys and values of a dictionary in one structure?

Single Answer MCQ
Q-00066657
View explanation
Q104

How would you clear all items from a dictionary `dict1`?

Single Answer MCQ
Q-00066658
View explanation
Q105

If `dict1 = {'x': 1, 'y': 2, 'z': 3}`, what will `for k in dict1:` output?

Single Answer MCQ
Q-00066659
View explanation
Q106

If you want to merge two dictionaries `dict1 = {'a': 1}` and `dict2 = {'b': 2}`, which method would you likely use?

Single Answer MCQ
Q-00066660
View explanation
Q107

What is the output of `dict1 = {'a': 1, 'b': 2}; print(dict1.get('b', 3))`?

Single Answer MCQ
Q-00066661
View explanation
Q108

What method would you use to retrieve all keys from a dictionary named 'data'?

Single Answer MCQ
Q-00066662
View explanation
Q109

Which of the following methods returns only the values of a dictionary?

Single Answer MCQ
Q-00066663
View explanation
Q110

If you want to check the existence of the key 'name' in the dictionary 'user', which statement would you use?

Single Answer MCQ
Q-00066664
View explanation
Q111

If a dictionary has nested dictionaries, which method could help to flatten or access inner values efficiently?

Single Answer MCQ
Q-00066665
View explanation
Q112

Which method adds a new key-value pair to an existing dictionary in Python?

Single Answer MCQ
Q-00066666
View explanation
Q113

When traversing a dictionary, what common error might you encounter if you modify the dictionary during iteration?

Single Answer MCQ
Q-00066667
View explanation
Q114

What will happen if you try to access a key that does not exist in a dictionary using the get() method?

Single Answer MCQ
Q-00066668
View explanation
Q115

What will be the output of the following code if executed? `dict1 = {'a': 1, 'b': 2}; for key in dict1.keys(): dict1[key] += 1; print(dict1)`?

Single Answer MCQ
Q-00066669
View explanation
Q116

If you delete a key 'age' from a dictionary, how can you confirm it has been removed?

Single Answer MCQ
Q-00066670
View explanation
Q117

What is the output of 'len({})' in Python?

Single Answer MCQ
Q-00066671
View explanation
Q118

How would you modify the value of an existing key in a dictionary?

Single Answer MCQ
Q-00066672
View explanation
Q119

Which function will return a list of all values in a dictionary called 'grades'?

Single Answer MCQ
Q-00066673
View explanation
Q120

What will be the output of the following code: 'd = {1: 'a', 2: 'b'}; print(d[1])'?

Single Answer MCQ
Q-00066674
View explanation
Q121

What is the main characteristic of a dictionary in Python?

Single Answer MCQ
Q-00066675
View explanation
Q122

You have a dictionary myDict and want to remove the item with key 'x'. Which of the following is correct?

Single Answer MCQ
Q-00066676
View explanation
Q123

What will 'd.get(5, 'Not Found')' return if key 5 does not exist?

Single Answer MCQ
Q-00066677
View explanation
Q124

How can you convert the keys of a dictionary named 'sampleDict' to a list?

Single Answer MCQ
Q-00066678
View explanation
Q125

In what scenario would using preset default values with get() be most useful?

Single Answer MCQ
Q-00066679
View explanation
Q126

How would you efficiently clear all items from a dictionary named 'cache'?

Single Answer MCQ
Q-00066680
View explanation
Q127

What does the len() function return when applied to a list?

Single Answer MCQ
Q-00104210
View explanation
Q128

Which method would you use to add an element at index 2 of a list?

Single Answer MCQ
Q-00104211
View explanation
Q129

What does the pop() method do when called with no arguments?

Single Answer MCQ
Q-00104212
View explanation
Q130

If list1 = [10, 20, 30, 20], what will list1.count(20) return?

Single Answer MCQ
Q-00104213
View explanation
Q131

What is the result of executing list1.reverse() on the list [5, 3, 8]?

Single Answer MCQ
Q-00104214
View explanation
Q132

When would you use the extend() method over append()?

Single Answer MCQ
Q-00104215
View explanation
Q133

What will result from executing list1.remove(10) on the list [10, 20, 10]?

Single Answer MCQ
Q-00104216
View explanation
Q134

Which method is used to determine the first index of a specific element in a list?

Single Answer MCQ
Q-00104217
View explanation
Q135

Which of the following methods does not modify the original list?

Single Answer MCQ
Q-00104218
View explanation
Q136

If list1 = [2, 3, 5], what will list1[1 + 1] return?

Single Answer MCQ
Q-00104219
View explanation
Q137

What will be the output of sorting the list ['d', 'c', 'b', 'a']?

Single Answer MCQ
Q-00104220
View explanation
Q138

Which of the following methods can cause a ValueError if the element is not found?

Single Answer MCQ
Q-00104221
View explanation
Q139

What will be the output of the following code: list1 = [1, 2, 3]; list1.sort(reverse=True)?

Single Answer MCQ
Q-00104222
View explanation
Q140

Which method removes all occurrences of a specified value from a list?

Single Answer MCQ
Q-00104223
View explanation
Q141

If you want to combine two lists into one list, which method would you use?

Single Answer MCQ
Q-00104224
View explanation

Working with Lists and Dictionaries Practice Worksheets

Practice questions from Working with Lists and Dictionaries to improve accuracy and speed.

Working with Lists and Dictionaries - Practice Worksheet

This worksheet covers essential long-answer questions to help you build confidence in Working with Lists and Dictionaries from Informatics Practices for Class 11 (Informatics Practices).

Practice

Questions

1

Define a list in Python. Explain its characteristics and provide examples of different types of lists.

A list in Python is an ordered collection of items, defined by square brackets. It is mutable, allowing modifications such as adding or removing elements. An example of a list with integers is: myList = [1, 2, 3]. A list can also contain mixed data types, such as myList = [1, 'Hello', 3.14]. Nested lists are also possible, like myNestedList = [[1, 2], ['a', 'b']].

2

What are the operations that can be performed on lists in Python? Explain with examples.

Basic operations on lists include concatenation, repetition, membership tests, slicing, and various built-in functions. For example, concatenation can be done using the + operator like list1 + list2. Repetition can be achieved using * like list1 * 3. Membership is checked with 'in', e.g., 5 in list1. Slicing allows sub-list creation, such as list1[1:3].

3

Describe how to access elements in a list using both positive and negative indexing. Provide examples.

You can access elements in a list by indexing, where indexing starts from 0 for the first element. For instance, myList[0] returns the first element, and myList[2] returns the third element. Negative indexing begins from the end of the list, where -1 accesses the last element. For example, if myList = [10, 20, 30], then myList[-1] returns 30.

4

Explain how Python lists support slicing and provide examples of different slicing techniques.

Slicing in Python allows you to extract a subset of a list. You can specify the start, stop, and step. For instance, myList[1:4] gets elements from index 1 to 3. Using myList[1::2] takes every second element from index 1 onwards. You can also slice in reverse using myList[::-1], which returns the list in reverse order.

5

What are dictionaries in Python? Discuss their characteristics with examples.

Dictionaries in Python are unordered collections of key-value pairs defined within curly braces. Each key is unique and maps to a value. An example is: myDict = {'name': 'Alice', 'age': 25}. Dictionaries are mutable, allowing modification of values, such as myDict['age'] = 26. Keys can be of any immutable data type.

6

How can you access items in a dictionary? Provide examples.

Items in a dictionary are accessed using keys. For example, if myDict = {'name': 'Bob', 'age': 30}, accessing myDict['name'] returns 'Bob'. If you try to access a key not present in the dictionary, it raises a KeyError. Use the 'get' method to avoid this error, as in myDict.get('height') which will return None if 'height' is not found.

7

Discuss the mutability of dictionaries and provide an example showing how to add and modify items.

Dictionaries are mutable, meaning you can add or change key-value pairs. For example, starting with myDict = {'Apple': 1}, you can add a new item with myDict['Orange'] = 2, resulting in myDict becoming {'Apple': 1, 'Orange': 2}. To modify, you can do myDict['Apple'] = 5, changing the value associated with 'Apple' to 5.

8

Explain dictionary methods such as keys(), values(), and items() with examples.

The keys() method returns a view object of all the keys in the dictionary. For example, myDict.keys() gives dict_keys(['name', 'age']). The values() method returns a view object of all values, such as myDict.values() producing dict_values(['Bob', 30]). The items() method returns a view of pairs, like myDict.items() resulting in dict_items([('name', 'Bob'), ('age', 30)]).

9

What is the difference between shallow copy and deep copy of a list? Provide examples.

A shallow copy of a list creates a new list but does not create copies of nested objects; it just copies references. For instance, if a = [[1, 2], [3, 4]], b = a.copy() modifies b[0][1] also affects a, showing they reference the same inner list. A deep copy, using import copy and copy.deepcopy(a), creates a completely independent copy, so changing b won't affect a.

10

How can you implement a program to count the frequency of elements in a list?

You can use a dictionary to track occurrences. Initialize an empty dictionary and iterate through the list, increasing counts for each element seen. For example: myList = [1, 2, 2, 3], for item in myList: count[item] = count.get(item, 0) + 1. This creates a frequency map indicating how many times each element appears.

Working with Lists and Dictionaries - Mastery Worksheet

This worksheet challenges you with deeper, multi-concept long-answer questions from Working with Lists and Dictionaries to prepare for higher-weightage questions in Class 11.

Mastery

Questions

1

Explain how lists are mutable in Python. Demonstrate this with an example that modifies elements within a list, and discuss the implications of mutability with references to data types.

Lists in Python are mutable, allowing us to change their contents after creation. For example, consider `myList = ['a', 'b', 'c']`. After the assignment, modifying `myList[1] = 'd'` results in `['a', 'd', 'c']`. This mutability signifies that the list reference can lead to unintended side effects if shared across functions.

2

Compare the use of the `append()` and `extend()` methods for lists with examples. Illustrate scenarios where each should be used.

The `append()` method adds a single element to a list, while `extend()` adds elements from an iterable to the end of the list. For instance: `listA = [1, 2]` after `listA.append([3, 4])` becomes `[1, 2, [3, 4]]`, but after `listA.extend([3, 4])`, it becomes `[1, 2, 3, 4]`. Use `append()` when you want to add a single element, and `extend()` when merging lists.

3

Discuss slicing in lists. Provide a comprehensive example that retrieves sublists using both positive and negative indexing.

Slicing allows us to obtain a subset of a list. For example, given `listA = [10, 20, 30, 40, 50]`, `listA[1:4]` returns `[20, 30, 40]`, while `listA[-3:]` gives `[30, 40, 50]`. Negative indexing enables access from the end of the list.

4

Create a program that shifts elements in a list by one position to the left. Include explanations of the hovering differences between index assignment and using a method like `insert()`.

To shift elements left, we can create a loop or slice. For example: `original = [1, 2, 3, 4]` results in `shifted = original[1:] + original[:1]`, giving `[2, 3, 4, 1]`. Index assignment modifies the list in place, while method calls like `insert()` can manipulate content differently.

5

Explain the concept of dictionaries in Python. How do key-value pairs support data retrieval? Provide an example that demonstrates adding, updating, and deleting keys.

Dictionaries store data as key-value pairs for quick access. For example: `dictA = {'name': 'John'}` shows `dictA['name']` returns 'John'. To add `dictA['age'] = 30` updates it to `{'name': 'John', 'age': 30}`. Deleting with `del dictA['name']` results in `{'age': 30}`.

6

Demonstrate the functionality of the `get()` and `pop()` methods in dictionaries. Compare their outputs with situations where keys may or may not exist.

`get()` retrieves a value without raising errors if a key does not exist, while `pop()` removes the key-value pair. For instance, in `dictB = {'x': 10}`, `dictB.get('y', 'default')` returns 'default', whereas `dictB.pop('y', 'not found')` raises a KeyError unless handled. This illustrates safe access vs. modification.

7

List and explain some built-in dictionary methods. Provide an example that showcases at least three of these methods in action.

Built-in methods such as `keys()`, `values()`, `items()`, and `update()` facilitate manageability of dictionaries. For example, `dictC = {'a': 1, 'b': 2}` shows `dictC.keys()` yields `dict_keys(['a', 'b'])`, whereas `dictC.update({'c': 3})` adds a new entry.

8

Write a Python function that merges two dictionaries. Discuss handling key collisions and strategies for prioritizing one dictionary’s values over another’s.

To merge two dictionaries, we can use `dictC.update(dictD)`. If a collision occurs, the latter dictionary retains its value. To prioritize `dictC`, one could iterate and handle keys: `for k in dictD: dictC.setdefault(k, dictD[k])`.

9

What are nested dictionaries? Provide an example where one dictionary contains another dictionary, and demonstrate accessing nested elements.

Nested dictionaries allow complex data structures. For instance, `dictE = {'person': {'name': 'Alice', 'age': 25}}` shows access through `dictE['person']['name']`, yielding 'Alice'. It’s crucial for modeling real-world data.

10

Apply list comprehensions to create a list that contains the squares of all even numbers between 1 and 20. Provide a commentary on the efficiency of such a method over traditional loops.

[x**2 for x in range(1, 21) if x % 2 == 0] generates [4, 16, 36, 64, 100, 144, 196] efficiently, as opposed to using loops, improving code clarity and performance. It’s a concise, readable manner to generate lists.

Working with Lists and Dictionaries - Challenge Worksheet

The final worksheet presents challenging long-answer questions that test your depth of understanding and exam-readiness for Working with Lists and Dictionaries in Class 11.

Challenge

Questions

1

Critically analyze the impact of list mutability in Python on data integrity. How can it lead to unintentional data loss in applications that require data permanence?

Discuss the implications of mutable data types such as lists, using examples of unintended mutation. Address safeguards that can be implemented to maintain data integrity.

2

Design a case study where you utilize a list of student marks to calculate and visualize their performance trends. How do different list manipulations affect the insight gathered?

Outline steps to analyze marks and present them, emphasizing operations like sorting, slicing, and calculations on lists.

3

Evaluate the trade-offs between using lists and dictionaries for mapping scores to student names in educational applications. When might one be preferred over the other?

Focus on time complexity, data retrieval efficiency, and clarity of code when using lists versus dictionaries.

4

Propose a solution using dictionaries to maintain a record of student grades that allows for frequent updates. What potential challenges could arise from this approach?

Identify methods to implement such a system and evaluate challenges like handling duplicates or concurrent modifications.

5

Create a program outline that uses both lists and dictionaries to manage a collection of books in a library system. What operations would you prioritize?

Discuss how lists can keep track of available books, while dictionaries map book details, focusing on the usefulness of each data structure for various operations.

6

Analyze a list manipulation error where an IndexError is raised. In what situations is this type of error most common, and how can you prevent it?

Explain the scenarios leading to IndexErrors, describe methods for prevention, and suggest best practices for safe list manipulation.

7

Discuss how slicing can be utilized to retrieve sublists of student data, such as honors and at-risk students, from a larger list of grades. What does this reveal about performance segmentation?

Provide a method to code this functionality and interpret the outcomes based on sliced data.

8

Critique the use of the 'in' keyword with lists versus dictionaries for membership testing in terms of efficiency and readability.

Evaluate both methods, providing examples and reasoning about their performance implications.

9

Explore advanced list methods such as extend, insert, and remove. How can improper use lead to data loss or corruption in a critical application?

Detail the operations' functionalities, pitfalls, and recommend careful coding practices.

10

Formulate a dictionary-based application that tracks student attendance. How could you optimize data storage and retrieval in a high-traffic environment?

Lay out a structure for storing attendance and evaluate how to ensure efficient access and updates.

Working with Lists and Dictionaries FAQs

Explore Chapter 4 of Informatics Practices on Working with Lists and Dictionaries. Learn about list operations, methods, and the use of dictionaries in Python programming.

A list in Python is an ordered collection of items, enclosed in square brackets and separated by commas. Lists can contain elements of different data types and can be modified after creation, making them versatile for various programming tasks.
Elements in a list can be accessed using their index, which starts at 0 for the first element. For example, list1[0] returns the first item. Negative indices can also be used to access elements from the end of the list.
List concatenation occurs when two or more lists are combined into one using the '+' operator. It creates a new list, merging the elements of the original lists, without altering them.
Mutability in the context of lists means they can be changed or modified after their creation. You can add, remove, or alter elements in a list without creating a new one.
List slicing allows you to obtain a subset of the list by specifying a range of indices. For example, list1[1:4] retrieves elements from index 1 to 3, returning a new list, while list1[:3] provides the first three elements.
Common list manipulation methods include append() to add an element, extend() for adding multiple elements from another list, insert() for inserting an element at a specific index, remove() to delete an element, and pop() to remove an element by index.
A dictionary in Python is a collection of key-value pairs, where each key is unique and maps to a specific value. Unlike lists, which are ordered collections, dictionaries are unordered, and values are accessed through their keys rather than numeric indices.
To add a new key-value pair to a dictionary, you can assign a value to a new key using the syntax: dictionary_name[new_key] = new_value. This will create a new entry in the dictionary.
You can check for the existence of a key in a dictionary using the 'in' operator. For example, 'key in dictionary_name' returns True if the key exists and False otherwise.
Built-in functions for dictionaries include len() to get the count of key-value pairs, keys() to retrieve a list of keys, values() to get a list of values, and items() to return a list of key-value tuple pairs.
The pop() method removes an element from a list by index and returns it. If no index is specified, it removes and returns the last element of the list.
Yes, a dictionary can have lists as values. Each key can map to a list, allowing you to store multiple related items.
Attempting to access a non-existing index in a list results in an IndexError. For example, trying to access list1[10] in a list with fewer than 11 elements will raise this error.
You can remove a key from a dictionary using the del statement. For example, 'del dictionary_name[key]' will delete the key and its associated value from the dictionary.
append() adds a single element to the end of a list, while extend() adds multiple elements from another iterable, effectively concatenating them to the end of the list.
An empty list can be created by using empty square brackets: list_name = []. Alternatively, you can use the list() function: list_name = list().
List repetition allows you to create multiple copies of the list's contents by using the '*' operator. For example, list1 * 3 creates a list that repeats all items in list1 three times.
Nested lists are lists that contain other lists as their elements. This allows for the creation of complex data structures, like matrices or tables.
Slicing with a step allows you to select items at intervals in the list using the syntax list[start:end:step]. For example, list[::2] picks every second element from the entire list.
A list can be sorted using sort() to sort it in place or sorted() to return a new sorted list without changing the original. You can also sort in descending order by adding reverse=True as an argument.
The count() method in lists returns the number of occurrences of a specified element. For example, list1.count('A') returns how many times 'A' appears in the list.
Lists are preferred for ordered collections where position matters, while dictionaries are used when you need to map unique keys to values without regard for position.

Working with Lists and Dictionaries Downloads

Download worksheets, revision guides, formula sheets, and the official textbook PDF for Working with Lists and Dictionaries.

Working with Lists and Dictionaries Official Textbook PDF

Download the official NCERT/CBSE textbook PDF for Class 11 Informatics Practices.

Official PDFEnglish EditionNCERT Source

Working with Lists and Dictionaries Revision Guide

Use this one-page guide to revise the most important ideas from Working with Lists and Dictionaries.

One-page review

Working with Lists and Dictionaries Practice Worksheet

Solve basic and application-based questions from Working with Lists and Dictionaries.

Basic comprehension exercises

Working with Lists and Dictionaries Mastery Worksheet

Work through mixed Working with Lists and Dictionaries questions to improve accuracy and speed.

Intermediate analysis exercises

Working with Lists and Dictionaries Challenge Worksheet

Try harder Working with Lists and Dictionaries questions that test deeper understanding.

Advanced critical thinking

Working with Lists and Dictionaries Flashcards

Test your memory with quick recall prompts from Working with Lists and Dictionaries.

These flash cards cover important concepts from Working with Lists and Dictionaries in Informatics Practices for Class 11 (Informatics Practices).

1/20

What is a list in Python?

1/20

A list is an ordered, mutable collection of elements, which can contain mixed data types, enclosed in square brackets and separated by commas.

How well did you know this?

Not at allPerfectly

2/20

How do you access the first element of a list?

2/20

Use the index 0 in square brackets, e.g., list_name[0].

How well did you know this?

Not at allPerfectly
Active

3/20

What does it mean that lists are mutable?

Active

3/20

It means that the contents of a list can be changed, added, or removed after it has been created.

How well did you know this?

Not at allPerfectly

4/20

What symbol is used to concatenate two lists?

4/20

The '+' operator is used to concatenate lists in Python.

5/20

Give an example of concatenating two lists.

5/20

list1 = [1, 2]; list2 = [3, 4]; list1 + list2 results in [1, 2, 3, 4].

6/20

What is the purpose of the '*' operator with lists?

6/20

It replicates the contents of the list, e.g., list1 * 3 results in repeating elements of list1 three times.

7/20

What does 'in' check in a list?

7/20

'in' checks if an element is present in the list and returns True or False.

8/20

What is list slicing?

8/20

Slicing allows you to create a new list by extracting a portion of the original list using index ranges.

9/20

What is the syntax for slicing a list?

9/20

list[start:stop] where 'start' is inclusive and 'stop' is exclusive.

10/20

What does negative indexing do in lists?

10/20

Negative indexing allows access to list elements from the end. For example, -1 accesses the last element.

11/20

How can you traverse a list?

11/20

You can traverse a list using a for loop or while loop.

12/20

Provide an example of traversing a list.

12/20

for item in list_name: print(item) outputs each element in the list.

13/20

How can you find the length of a list?

13/20

Use the len() function, e.g., len(list_name) returns the number of elements in the list.

14/20

What happens if you access an out-of-range index?

14/20

An IndexError is raised indicating that the list index is out of range.

15/20

Can lists contain different data types?

15/20

Yes, lists can contain elements of multiple data types, like integers, strings, and even other lists.

16/20

What is a nested list?

16/20

A nested list is a list that contains other lists as its elements.

17/20

Provide an example using membership operator.

17/20

'Green' in ['Red', 'Green', 'Blue'] returns True.

18/20

How do you slice a list with steps?

18/20

Use the syntax list[start:end:step], where 'step' determines the interval between elements in the slice.

19/20

What is list manipulation?

19/20

It involves adding, removing, or altering elements within a list to change its contents.

20/20

Name three common operations you can perform on lists.

20/20

Concatenation, repetition, and membership testing are common list operations.

Show all 20 flash cards

Practice mode

Live Academic Duel

Master Working with Lists and Dictionaries via Live Academic Duels

Challenge your classmates or test your individual retention on the core concepts of CBSE Class 11 Informatics Practices (Informatics Practices). Compete in speed-recall question rounds matched explicitly to the latest syllabus milestones for Working with Lists and Dictionaries.

CBSE-aligned questions
Instant speed-recall rounds

Quick, competitive practice on Working with Lists and Dictionaries with zero setup.