TN Online TestSamacheer Kalvi practice

Lists, Tuples, Sets and Dictionary - Study Notes

Share this chapter: Telegram

Chapter Summary

Python has four built-in collection types, and this chapter covers all of them: List, Tuple, Set and Dictionary. Each one lets you keep many values under a single name. A List is an ordered sequence that can be changed. A Tuple is an ordered sequence that cannot be changed. A Set holds only unique elements and keeps no fixed order. A Dictionary stores key-value pairs, where each key leads to one value. Most exam questions in this chapter come down to one thing: which of these can be changed, and which brackets are used to write it.

Learning Objectives

Key Concepts and Definitions

Lists

A List is a sequence of elements written inside square brackets [ ]. It is ordered, so each element keeps the position it was placed in. It is mutable, so elements can be replaced, added or deleted while the program runs. Positive indexes start at 0 from the front. Negative indexes start at -1 from the end, which is called reverse indexing.

Tuples

A Tuple is a sequence of elements separated by commas, usually written inside parentheses ( ), though the parentheses are optional. It is ordered but immutable: once created, its elements cannot be changed. Because Python does not have to allow for changes, a Tuple is processed faster than a List.

Sets

A Set is a collection of unique elements written inside curly braces { }, with no fixed order. Duplicate values are not kept. Sets are handy for checking whether a value is present and for removing repeated values from a sequence. Note that empty braces {} create an empty Dictionary, not a Set; use set() for an empty Set.

Dictionaries

A Dictionary is a mutable collection of key-value pairs written inside curly braces { }. A colon (:) separates each key from its value, and every key must be unique. The key works like your own index, so you look up a value by its key instead of by position.

Worked Methods

1. Creating and Updating Lists

Create a List with square brackets. Use append() to add one element at the end and extend() to add several elements at once. Use insert(index, element) to place an element at a given position. To change an existing element, assign to its index: list_var[index] = new_value.

2. List Comprehension

List comprehension builds a new list in a single line. The general form is [expression for variable in range]. For example, squares = [x**2 for x in range(1, 11)] gives the squares of 1 to 10.

3. Tuple Assignment

In tuple assignment, the variables on the left of = receive the values on the right, in order. For example, (x, y, z) = (10, 20, 30) puts 10 in x, 20 in y and 30 in z.

4. Set Operations

Sets support the operations you know from mathematics:

Common Exam Traps

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions12 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python Variables and Operators 6 Control Structures 7 Python Functions 8 Strings and String Manipulation 10 Python Classes and Objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV Files 14 Importing C++ Programs in Python 15 Data Manipulation through SQL 16 Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart