Professional Documents
Culture Documents
Data Struc Reviewer Lim
Data Struc Reviewer Lim
Page | 1
data structures reviewer | lim_shin
NOTE:
Page | 2
data structures reviewer | lim_shin
• Extract a portion of the list The range function returns a sequence of numbers.
• Reverse a list By default, it returns starting from 0 to the specified
number (increments by 1). The starting and ending
• Slicing with a step
values can be passed according to our needs.
• Slice without specifying start or end position
ITERATING A LIST
Page | 3
data structures reviewer | lim_shin
USING extend()
Page | 4
data structures reviewer | lim_shin
Note: It will remove the last time from the list if the
index number is not passed.
Page | 5
data structures reviewer | lim_shin
Use del keyword along with list slicing to remove Use the index() function to find an item in a list.
the range of items
The index() function will accept the value of the
element as a parameter and returns the first
occurrence of the element or returns ValueError if
the element does not exist.
Page | 6
data structures reviewer | lim_shin
There are two ways by which a copy of a list can be The copy method can be used to create a copy of a
created. Let us see each one with an example. list. This will create a new list and any changes
made in the original list will not reflect in the new
Using assignment operator (=)
list. This is shallow copying.
This is a straightforward way of creating a copy. In
this method, the new list will be a deep copy. The
changes that we make in the original list will be
reflected in the new list.
LIST OPERATIONS
As seen in the above example a copy of the list has We can perform some operations over the list by
been created. The changes made to the original list using certain functions like sort(), reverse(),
are reflected in the copied list as well. clear() etc.
Note: When you set list1 = list2, you are Sort List using sort()
making them refer to the same list object, so
The sort function sorts the elements in the list in
when you modify one of them, all references
ascending order.
associated with that object reflect the current state
of the object. So don’t use the assignment operator
to copy the dictionary instead use the copy()
method.
OUTPUT:
Page | 7
data structures reviewer | lim_shin
Reverse a List using reverse() We can even create a list when the input is a
continuous range of numbers.
The reverse function is used to reverse the
elements in the list.
OUTPUT:
OUTPUT:
outputList = {expression(variable) for variable in l1 = [10, 20, 30, 40, 50] and l2 = [60,
inputList [if variable condition1][if variable 70, 80, 60]
condition2]
OUTPUT:
Page | 8
data structures reviewer | lim_shin
• Attribute
• Behavior
Page | 9
data structures reviewer | lim_shin
Page | 10
data structures reviewer | lim_shin
Page | 11
data structures reviewer | lim_shin
TYPES OF CONSTRUCTORS
1. Default Constructor
2. Non-parametrized constructor
3. Parameterized constructor
OUTPUT:
Page | 12
data structures reviewer | lim_shin
NOTE:
OUTPUT:
PARAMETERIZED CONSTRUCTOR
Page | 13
data structures reviewer | lim_shin
OUTPUT:
OUTPUT:
Page | 14
data structures reviewer | lim_shin
OUTPUT:
Page | 15
data structures reviewer | lim_shin
• The constructor executes when we create the Exercise 1: Create a list by picking an odd-index
object of the class. For every object, the items from the first list and even index items from
constructor is called only once. the second
• So, for counting the number of objects of a
Given two lists, l1 and l2, write a program to create
class, we can add a counter in the constructor,
a third list l3 by picking an odd-index element from
which increments by one after each object
the list l1 and even index elements from the list l2.
creation.
SOLUTION:
➢ for 1st list: Start from the 1st index with step
OUTPUT: value 2 so it will pick elements present at
index 1, 3, 5, and so on
➢ for 2nd list: Start from the 0th index with
step value 2 so it will pick elements present
CONSTRUCTOR RETURN VALUE at index 0, 2, 4, and so on
• In Python, the constructor does not return any
value. Therefore, while declaring a constructor,
we don’t have anything like return type.
• Instead, a constructor is implicitly called at the
time of object instantiation. Thus, it has the sole
purpose of initializing the instance variables.
• The __init__() is required to return None. We
can not return something else. If we try to return
a non-None value from the __init__()
method, it will raise TypeError.
OUTPUT:
OUTPUT:
Page | 16
data structures reviewer | lim_shin
SOLUTION:
OUTPUT:
OUTPUT:
OUTPUT:
Page | 17
data structures reviewer | lim_shin
SOLUTION:
OUTPUT:
Page | 18
data structures reviewer | lim_shin
Page | 19