0% found this document useful (0 votes)
115 views51 pages

List, Tuple, Set, Dictionary

The document discusses lists, tuples, sets, and dictionaries in Python. It provides: 1) An introduction to each data type, explaining their properties like mutability and ordering; 2) How to create objects of each type using various methods; and 3) Important functions for each type, including accessing, manipulating, and traversing their elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views51 pages

List, Tuple, Set, Dictionary

The document discusses lists, tuples, sets, and dictionaries in Python. It provides: 1) An introduction to each data type, explaining their properties like mutability and ordering; 2) How to create objects of each type using various methods; and 3) Important functions for each type, including accessing, manipulating, and traversing their elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

List, Tuple, Set,

Dictionary
Objective

Able to write programs using list,


tuple, dictionary and set
List
Introduction
• Using a list, a programmer can use a single variable to store all the
elements of the same or different data type and even print them.
• A list is a sequence of values called items or elements.
• The elements can be of any type.
• A list is mutable in nature; the contents can be changed.
• The user can increase or decrease its size; thus it is dynamic in nature.
• The elements are placed within a square bracket, differentiated by a
comma separator.
Creating of List Objects
The list class defines lists. A programmer can use a list’s
constructor to create a list. The empty list object can be
created as follows:
Creating of List Objects (with Dynamic Input)

Creating of List Objects (with list function)


Creating of List Objects (with split() function)
Accessing Elements of a List
The elements of a list are unidentifiable by their positions. Hence, the
index[] operator is used to access them.
Accessing Elements of a List (By using an index)

Accessing Elements of a List (By using a slice operator)


List vs Mutability
After the creation of a list, you can modify its
contents; hence it is mutable in nature.
Traversing the Elements of a List
Traversal means to access the elements of a list sequentially.
Traversing the Elements of a List
Important Functions of a List
Getting the information about the list
• len()
• count()
• index()
Important Functions of a List
Manipulating Elements of a List
• append() - This function is used to add new items at the end of a list
• insert() - It is used to insert an item at a specified index position.
Important Functions of a List
Manipulating Elements of a List
Important Functions of a List
Manipulating Elements of a List
• extend() - add the items of a list into another list
Important Functions of a List
Manipulating Elements of a List
• remove() - remove particular items from the list
• pop() – remove and return the last element of the list
Important Functions of a List
Ordering Elements of a List
• reverse() – reverses the elements of the list
• sort() – sorts the elements of data
Using Mathematical Operators of List
Two operators are used for list objects; + and *
Membership Operators
Membership operators are used to check whether the element is a
member of the list or not.
The clear() function
The clear() function is used to remove all the elements from the list.
Nested list
Nested list
List comprehensions
List comprehension is used to create a new list from existing sequences. It
is a tool for transforming a given list into another list.
Tuple
Introduction
• A tuple contains a sequence of items of many types.
• The elements of tuples are fixed.
• Once a tuple has been created, we cannot add or delete elements or
shuffle their order.
• Tuples are immutable
Tuple Creation
A tuple is an inbuilt data type in Python. In order to create a tuple, the
elements of tuples are enclosed in parenthesis instead of square brackets.
Accessing Elements of a Tuple
The elements of a tuple can be accessed by the index and slice
operator.
Tuple vs Immutability
A tuple is immutable in nature; you cannot modify the contents of a tuple
once created.
Mathematical Operators for tuple
Important functions of tuple
Important functions of tuple
Tuple Packing and Unpacking
A tuple can be created by packing the set of variables.
Set
Introduction
• A set is an unordered collection of unique elements without duplicates.
• A set is mutable.
• We can easily add or remove elements from a set.
• The set data structure in Python is used to support mathematical set
operations.
• Set is an unordered and unindexed collection of items in Python.
Unordered means when we display the elements of a set, it will come
out in random order.
• Unindexed means, we cannot access the elements of a set using the
indexes like we can do in list and tuples.
Create of Set Objects
A programmer can create a set by enclosing the elements inside a pair of
curly brackets{}.
Create of Set Objects
Important Functions of a Set
Important Functions of a Set
Set comprehension
The indexing and slicing operations are not supported by set
objects.
Dictionary
Introduction
• A dictionary is a collection that stores values along with keys.
• The sequence of such keys and value pairs is separated by commas.
• These pairs are sometimes called entries.
• If you wish to show the set of objects as a (key, value) pair, then
dictionaries are preferred.
Create a Dictionary
• We can create a dictionary by enclosing the items inside a pair of curly
brackets{}.
• One way to start a dictionary is to create an empty dictionary first and
then add items to it.
Accessing Data from a Dictionary
The data can be accessed using keys.
Updating a Dictionary
If you wish to add any new (key, value) pair in the already defined
dictionary, then the following syntax is used:
Deleting Elements from a Dictionary
Important Functions of a Dictionary
a) len(): It gives the quantity of elements present in the dictionary.
b) clear(): It will delete the complete elements from dictionary.
c) get(): It will return the associated value of the key if it is present or
none will be returned.
Important Functions of a Dictionary
Important Functions of a Dictionary
Dictionary comprehension
The comprehension concept is applicable in dictionaries.

You might also like