UNIT 2
Data Structures in Python
Data Structures:
Lists : Basic list operations, Replacing, inserting, removing an element; Searching and sorting a
list, Methods of list objects, using lists as Stacks and Queues, how efficient lists are when used
as stack or queue, List, and nested list Comprehensions
Tuple, Sets, Difference between list and tuple
Dictionary - adding and removing keys, accessing, and replacing values, traversing
dictionaries.
UNIT 2: LIST
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are Tuple, Set, and Dictionary, all with
different qualities and usage.
Lists are created using square brackets:
Example
alpha = ["a", "b", "c"]
print(alpha)
List Items
List items are ordered, changeable, and allow duplicate
values.
List items are indexed, the first item has index [0], the
second item has index [1] etc.
Ordered
The items have a defined order, and that order will
not change.
If you add new items to a list, the new items will be
placed at the end of the list.
Note: There are some list methods that will change the order, but in
general: the order of the items will not change.
Changeable
The list is changeable, meaning that we can change, add,
and remove items in a list after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same
value.
Example
Lists allow duplicate values:
alpha = ["a", "b", "c", "a", "c"]
print(alpha)
List Length
To determine how many items a list has, use the
len() function:
Example
alpha = ["a", "b", "c", "a", "c"]
print(len(alpha))
List Items - Data Types
List items can be of any data type
Example
String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
A list can contain different data types:
Example
A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]
type()
Lists are defined as objects with the data type
'list':
<class 'list'>
Example
alpha = ["a", "b", "c", "a", "c"]
print(type(alpha))
The list() Constructor
It is also possible to use the list() constructor when creating a new list.
Example
Using the list() constructor to make a List:
alpha = list(("a", "b", "c"))
# note the double round-brackets
print(alpha)
Access List Items
Access Items
List items are indexed and you can access them by referring to the
index number:
alpha = ["a", "b", "c"]print(alpha[1])
Negative Indexing
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item etc.
alpha = ["a", "b", "c"]print(alpha[-1])
Range of Indexes
You can specify a range of indexes by specifying where to start and
where to end the range.
When specifying a range, the return value will be a new list with the
specified items.
alpha = ["a", "b", "c“, ”d”, ”e”]
print(alpha[1:4])
To change the value of a specific item, refer to the index number:
alpha = ["a", "b", "c"]
alpha[1] = “B"
print(alpha)
Change a Range of Item Values
To change the value of items within a specific range, define a list with
the new values, and refer to the range of index numbers where you
want to insert the new values:
alpha = ["a", "b",
"c“,”d”,”e”,”f”,’’g’]
alpha[1:3] = [“BB”,”CC”]
print(alpha)
alpha = ["a", "b",
"c“,”d”,”e”,”f”,’’g’]
alpha[1:2] = [“BB”,”CC”]
print(alpha)
alpha = ["a", "b", “c”]
alpha[1:3] = [“ZZ”]
print(alpha)
Add List Items
Append Items
To add an item to the end of the list, use the append() method:
alpha = ["a", "b", “c”]
alpha.append(“d”)
print(alpha)
Insert Items
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
alpha = ["a", "b", “c”]
alpha.insert(1, “d”)
print(alpha)
Extend List
To append elements from another list to the current list, use the
extend() method.
alpha = ["a", "b", “c”]
num = [1,2,3]
alpha.extend(num)
print(alpha)
Remove List Items
The remove() method removes the specified item.
alpha = ["a", "b", “c”]
alpha.remove(“b”)
print(alpha)
Remove Specified Index
The pop() method removes the specified index.
alpha = ["a", "b", “c”]
alpha.pop(1)
print(alpha)
Note : If you do not specify the index, the pop() method removes the
last item.
The del keyword also removes the specified index:
alpha = ["a", "b", “c”]
del alpha[0]
print(alpha)
The del keyword can also delete the list completely.
alpha = ["a", "b", “c”]
del alpha
print(alpha)
Clear the List
The clear() method empties the list.
The list still remains, but it has no content.
alpha = ["a", "b", “c”]
alpha.clear()
print(alpha)
Loop Through a List
You can loop through the list items by using a for loop:
alpha = ["a", "b", “c”,”d”,”e”]
for x in alpha
print(x)
Loop Through the Index Numbers
loop through the list items by referring to their index number.
alpha = ["a", "b", “c”,”d”,”e”]
for i in range(len(alpha))
print(alpha[i])
Using a While Loop
LIST COMPREHENSIONS
List comprehension offers a shorter syntax when you want to create a
new list based on the values of an existing list.
Syntax
newList = [ expression(element) for element in oldList if condition ]
Example:
List1 = [10,20,30,40,50]
List2= [x+10 for x in List1]
print(List2)
Write a program to create a list with elements 1,2,3,4 and 5. Display even
elements of the list using list comprehension
Consider a list with five different Celsius values.
Convert all the Celsius values into Fahrenheit. Using
List Comprehension
Formula :
Fahrenheit = (9/5)*celsius + 32
Consider the list with mixed type of elements, such as
L1=[1,’a’, 3.45,2,’b’,8,2.5]. Create another list using list
comprehension which consists of only the integer elements presents
within the list L1
List Methods
append(object x)
clear()
count(object x)
copy()
extend(list 2)
index(object x)
insert(int index, object x)
reverse()
sort()
len()
max()
min()
sum()
Design, Develop and Implement a menu driven Program in Python
for the
following operations on STACK
(List Implementation of Stack )
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Design, Develop and Implement a menu driven Program in Python for the
following operations on Queue (List Implementation of Queue with
maximum size MAX)
A. Insert an Element on to Queue
B. Delete an Element from Queue
C. Demonstrate Overflow and Underflow situations on Queue
D. Display the status of Queue
E. Exit