Unit II
Unit II
List
Creating Lists
• Lists are constructed using square brackets [ ] wherein you can
include a list of items separated by commas.
Examples
Basic List Operations
• In Python, lists can also be concatenated using the + sign, and the *
operator is used to create a repeated sequence of list items.
Check Presence of Element
• You can check for the presence of an item in the list using in and not
in membership operators. It returns a Boolean True or False.
The list() Function
• The built-in list() function is used to create a list.
• The syntax for list() function is,
list([sequence])
The list() Function
Indexing and Slicing in Lists
• As an ordered sequence
of elements, each item in
a list can be called
individually, through
indexing.
Modifying Items in Lists
• Lists are mutable in nature as
the list items can be modified
after you have created a list.
Assigning new List
• When you assign an existing list variable to a new variable, an
assignment (=) on lists does not make a new copy.
Slicing
• Slicing of lists is allowed in Python wherein a part of the list can be
extracted by specifying index range along with the colon (:) operator
which itself is a list.
Built-In Functions Used on Lists
Built-In Functions
Used on Lists
List Methods
• The list size changes dynamically whenever you add or remove the
items and there is no need for you to manage it yourself.
• To do with things inside the List
List Methods
List Methods - Example
Populating Lists with Items
• One of the popular way of populating lists is to start with an empty
list [ ], then use the functions append() or extend() to add items to
the list.
Example
Traversing of Lists
Nested Lists
Nested Lists
Example Program
• Write a Program to find the transpose of a matrix
The del Statement
• You can remove an item from a list based on its index rather than its
value.
• The difference between del statement and pop() function is that the
del statement does not return any value while the pop() function
returns a value.
• The del statement can also be used to remove slices from a list or
clear the entire list.
The del Statement
Dictionary
Creating Dictionary
• A dictionary is a collection of an unordered set of key:value pairs, with
the requirement that the keys are unique within a dictionary.
• Dictionaries are constructed using curly braces { }, wherein you
include a list of key:value pairs separated by commas.
Example
Cont.,
• Dictionary keys are immutable type and can be either a string or a
number.
• Since lists can be modified in place using index assignments, slice
assignments, or methods like append() and extend(), you cannot use
lists as keys.
• Duplicate keys are not allowed in the dictionary.
Accessing and Modifying key:value Pairs in
Dictionaries
• Each individual key:value pair in a dictionary can be accessed through
keys by specifying it inside square brackets.
• The key provided within the square brackets indicates the key:value
pair being accessed.
• The syntax for accessing the value for a key in the dictionary is,
• The syntax for modifying the value of an existing key or for adding a
new key:value pair to a dictionary is,
Example
Built-In Functions Used on Dictionaries
Dictionary Methods
• Dictionary allows you to store data in key:value format without
depending on indexing.
Dictionary Methods
Dictionary Methods
Populating Dictionaries with key:value Pairs
• One of the common ways of populating dictionaries is to start with an
empty dictionary { }, then use the update() method to assign a value
to the key using assignment operator.
Traversing of Dictionary
The del Statement
• To delete the key:value pair, use the del statement followed by the
name of the dictionary along with the key you want to delete.
Tuples and Sets
Tuples and Sets
• In mathematics, a tuple is a finite ordered list (sequence) of elements.
• A tuple is defined as a data structure that comprises an ordered, finite
sequence of immutable, heterogeneous elements that are of fixed
sizes.
• Often, you may want to return more than one value from a function.
• Tuples solve this problem.
• They can also be used to pass multiple values through one function
parameter.
Creating Tuples
• A tuple is a finite ordered list of values of possibly different types
which is used to bundle related values together without having to
create a specific type to hold them.
• Tuples are immutable.
• Once a tuple is created, you cannot change its values.
Example
Tuples
• You can store any item of type string, number, object, another
variable, and even another tuple itself.
• You can have a mix of different types of items in tuples, and they
need not be homogeneous.
Basic Tuple Operations
• Like in lists, you can use the + operator to concatenate tuples
together and the * operator to repeat a sequence of tuple items.
The tuple() Function
• The built-in tuple() function is used to create a tuple.
• The syntax for the tuple() function is,
• where the sequence can be a number, string or tuple itself.
• If the optional sequence is not specified, then an empty tuple is
created.
Example
Example
Indexing and Slicing in Tuples
• The syntax for accessing an item in a tuple is,
Slicing of tuples
Example
Built-In Functions Used on Tuples
Examples
Relation between Tuples and Lists
• Though tuples may seem similar to lists, they are often used in
different situations and for different purposes.
• Tuples are immutable, and usually, contain a heterogeneous
sequence of elements that are accessed via unpacking or indexing.
• Lists are mutable, and their items are accessed via indexing.
• Items cannot be added, removed or replaced in a tuple.
Relation between Tuples and Dictionaries
Tuple Methods
Tuple Methods
Example
Tuple Packing and Unpacking
Traversing of Tuples
Populating Tuples with Items
• You can populate tuples with items using += operator and also by
converting list items to tuple items.
Using zip() Function
• The zip() function makes a sequence that aggregates elements from
each of the iterables (can be zero or more).
• The syntax for zip() function is,
Sets
Sets
• Python also includes a data type for sets.
• A set is an unordered collection with no duplicate items.
• Primary uses of sets include membership testing and eliminating
duplicate entries.
• Sets also support mathematical operations, such as union,
intersection, difference, and symmetric difference.
• Curly braces { } or the set() function can be used to create sets with a
comma-separated list of items inside curly brackets { }.
Example
Sets
• Sets are mutable. Indexing is not possible in sets, since set items are
unordered.
• You cannot access or change an item of the set using indexing or
slicing.
Set Methods
Set Methods
Traversing of Sets
Frozenset
• A frozenset is basically the same as a set, except that it is immutable.
• Once a frozenset is created, then its items cannot be changed. Since
they are immutable, they can be used as members in other sets and
as dictionary keys.
• The frozensets have the same functions as normal sets, except none
of the functions that change the contents (update, remove, pop, etc.)
are available.