You are on page 1of 42

UNIT-4(LIST,TUPLE)

LIST: INTRODUCTION
 List is a fundamental data structure that serves
as a container for holding an ordered collection
of items/objects. The items of a list need not be
of same data type.
 We can retrieve the elements of a list using
"index".
 List can be mixture of types like numbers,
strings and other lists as well.
 They are mutable which means the elements in
the list can be changed/modified
 Let us consider an example:
 l1= [56, 78.94, "India"]
LIST CAN BE CREATED BY TWO
WAYS
SPLIT()
 We are using split() function of string, which
converts a string into list.

 Abc=‘hello’
 A=print(Abc.SPLIT())

 Output:<class,’list’>
DIFFERENT TYPE OF LISTS
LIST OPERATIONS
MEMBERSHIP OPERATOR
REPETITION, CONCATENATION
AND EXTEND()
COMPARISON
MUTABILITY: LIST ITEMS CAN BE CHANGED
BY ASSIGNING NEW VALUES USING THE
INDEXING OPERATOR ([]).
DELETE OPERATIONS ON A LIST
TUPLES
 A tuple is similar to a list in Python, but it is
immutable, meaning its elements cannot be
changed after creation, providing a fixed and
ordered collection of values. Elements of a
tuple are enclosed in parenthesis ( ).

Once a tuple has been created, addition or


deletion of elements to a tuple is not
possible due to its immutable nature.
IDENTIFY CORRECT OPTIONS
 A tuple is a mutable list.
 Tuples can be used as keys in dictionaries.
 Elements of a tuple are enclosed in
parenthesis.
 Addition and deletion of elements is possible
in a tuple.
 It is possible to create tuples which contain
mutable objects, such as lists.
DICTIONARY
 A dictionary is an unordered collection of elements in the form
of key : value pairs.

The keys of a dictionary should be unique and the values can


change. So we use immutable data types(Number, string, tuple
etc.) for the key and any type for the value.

Unlike sequences which are indexed by a range of numbers,


dictionaries are indexed by keys.
 The elements of a dictionary are enclosed within { } braces.
 Tuples with same data type elements can be used as keys of a
dictionary.
 If any tuple contains a mutable object (such as a list) as an
element, the tuple cannot be used as a key.
 Usually a pair of braces { } represents an empty dictionary.
 Elements can be added, modified or removed using the key.
IDENTIFY CORRECT OPTIONS
 The keys in a dictionary are mutable.
 The values of a dictionary should be
immutable.
 A dictionary is a collection of unordered
elements and each element will in the form
a key: value pair.
 A tuple can be a key, if it only contain
strings, numbers and lists.
 The dict() function is used to construct a
dictionary.
THE TYPICAL OPERATIONS THAT CAN
BE PERFORMED ON A DICTIONARY
ARE:

Add elements i.e., key:value pairs.



Access elements using the key with the index operator [ ] or
using get() method.

Update the value of a particular element given in the
dictionary.

The elements can also be deleted which results in deletion
of both the key:value pair and we can also delete the entire
dictionary using del() method.

Iterate through a dictionary for processing the elements in it
ZIP FUNCTION
 Zip function is used when more than one
list,tuple,and set iterate at the same time.
 We can create dictionary from two lists by
zip function.
DICTIONARY METHODS
SET
 Set is an unordered collection that contains unique
elements.
The elements within a set are immutable, which
means they cannot be changed after being added. As a
result, a set cannot include mutable elements such
as lists, sets or dictionaries.
 The set is mutable which means elements can be
added or removed from it.

 The main operations that can be performed on a set


are:Membership test
 Eliminating duplicate entries.
 Mathematical set operations like union, intersection,
difference and symmetric difference.
IDENTIFY CORRECT OPTIONS
 It allows duplicate elements, ensuring
comprehensive data representation.
 It stores only unique elements and offers
various set operations.
 It can include mutable elements like lists
and dictionaries, enhancing flexibility.
 It maintains the order of elements, enabling
efficient indexing
SET METHODS
IT CHECK WHETHER THE SET A
IS PART OF SET B
LIST COMPREHENSION
EXAMPLE
SET COMPREHENSION
 Set Comprehension returns the result as a
set. They are enclosed within curly
braces { } and contain an expression followed
by a for clause. They may also be followed
by additional for clauses or if clauses. or if
clauses.
DICTIONARY COMPREHENSION
 Sybtax: <dict_name> =
{<new_key>:<new_value> for <item> in
<iterable>}
 {} indicates that we're populating a
dictionary.
 For each item in the iterable, we generate a
key-value pair in the dictionary.

You might also like