TN Online TestSamacheer Kalvi practice

Lists, Tuples, Sets and Dictionary - Study Notes

Share this chapter: Telegram

Chapter Summary

This chapter explores the four primary built-in data collections in Python: Lists, Tuples, Sets, and Dictionaries. These structured data types allow programmers to group multiple items together under a single identifier. A List is an ordered, mutable sequence. A Tuple is an ordered, immutable sequence. A Set is an unordered collection of unique elements. A Dictionary is an unordered collection of key-value pairs where each key maps to a specific value. Mastering these collections is critical for managing data, implementing algorithms, and building robust software applications in Python.

Learning Objectives

Key Concepts and Definitions

Lists

A List is a sequence of elements enclosed within square brackets [ ]. Lists are ordered, meaning elements stay in their inserted position. They are mutable, allowing elements to be replaced, added, or deleted during execution. Elements are indexed starting from zero for positive subscripts, and from -1 for negative subscripts (reverse indexing).

Tuples

A Tuple is a sequence of elements separated by commas and optionally enclosed within parentheses ( ). Tuples are ordered but immutable, meaning their elements cannot be changed after creation. This immutability ensures data integrity and enables faster execution compared to Lists.

Sets

A Set is an unordered collection of distinct, unique elements enclosed in curly braces { }. Sets do not allow duplicate values. They are highly efficient for membership testing and eliminating duplicates from sequences.

Dictionaries

A Dictionary is a mutable collection of key-value pairs enclosed in curly braces { }. Each key must be unique and is separated from its associated value by a colon (:). Keys act as customized indexes for rapid data lookup.

Worked Methods

1. Creating and Updating Lists

Lists are created using square brackets. To add a single element, use the append() method. To add multiple elements, use the extend() method. To insert an element at a specific index, use insert(index, element). Elements can be updated via assignment: list_var[index] = new_value.

2. List Comprehension

List comprehension offers a compact syntax for generating new lists. The general structure is [expression for variable in range]. For example, to generate a list of squares: squares = [x**2 for x in range(1, 11)].

3. Tuple Assignment

Tuple assignment allows variables on the left side of the assignment operator to be mapped to values on the right. For example: (x, y, z) = (10, 20, 30) assigns 10 to x, 20 to y, and 30 to z.

4. Set Operations

Sets support mathematical operations:

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