Unit-4-List Tuple Dictonary
Unit-4-List Tuple Dictonary
LIST
This Session will discuss about …
• Concept of a collection
• Lists and definite loops
• Indexing and lookup
• List mutability
• Functions: len, min, max, sum
• Slicing lists
• List methods
• Sorting lists
• Splitting strings into lists of words
• Using split to parse strings
CREATED BY K. VICTOR BABU
Lists
• .
Split breaks a string into parts and produces a list of strings. We think of these as
words. We can access a particular word or loop through all the words.
CREATED BY K. VICTOR BABU
Cloning or Copying a list
lst1=[10,20,30,40]
lst2 = copy.copy(lst1)
print("lst1:",lst1)
print("lst2:",lst2)
Output:
lst1: [10, 20, 30, 40]
lst2: [10, 20, 30, 40]
# Driver Code
lst1 = [4, 8, 2, 10, 15, 18]
lst2 = Cloning(lst1) #passing list as a
argument
print("Original List:", lst1)
print("After Cloning:", lst2)
Output:
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
CREATED BY K. VICTOR BABU
MultiDimensional
• Lists are ofLists
arbitrary length and and easily be
nested.
• Simplest nested lists are 2 –dimensional matrices.
• my2DList = [[1,2,3,4],[5,6,7,8],[9,10,11,12],
[13,14,15,16]]
my2DLis
t 0 1
2
0 1 5 9 13 3
1 2 6 10 14
2 3 7 11 15
3 4 8 12 16
0 1 2 3
my2DLis
t
0 1 5 9 beta
1 2 6 10 14
2 3 7 e 15
3 a cat 12 16
0 1 2 3
my2DLis
t
0 1 5 9 beta
1 2 6 10 14
2 3 7 e 15
3 a 12 16
0 1 5 9 bet
a
1 2 6 10 1
4
0 1
2 3 7 e 1
5 0 p q
3 a cat 12 1 r s
• myList = [’Name’,[Month,Date,Year],Address,
[Home,Cell]]
Str [Ref] Str [Ref]
int Str
int Str
int
• myList = [’Name’,[Month,Date,Year],Address,
[Home,Cell]]
[Ref] [Ref] [Ref] [Ref]
int Str
int
[[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’z’],[‘r’,’s’]]]]
(a) numbers[-1::]
(b) numbers[::-1]
(c) numbers[:-1:]
(d) numbers[9:8:1]
(a) [2,3,4,5]
(b) [9,8,7,6]
(c) [5,4,3,2]
(d) [6,7,8,9]
(a) [0, 0, 0, 0]
(b) [0][0][0][0]
(c) [0],[0],[0].[0]
(d) [4,4,4,4]
4. Predict output ?
>>> [1, 2, 3] * 3
(a) [3,6,9]
(b) [1,2,3,1,2,3,1,2,3]
(c) [1,2,3,4,5,6,7,8,9]
(d) [4,5,6,7,8,9]
(a) [3,6,9]
(b) [1,2,3,1,2,3,1,2,3]
(c) [1,2,3,4,5,6,7,8,9]
(d) [4,5,6,7,8,9]
(a) [1,2,3,4,5,6]
(b)[1,4,9,16,25,36]
(c) [1,4,9,16,25]
(d)[]
8. Predict output ?
>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
(a) [10,20,30,40]
(b)[80,60,70]
(c) [40]
(d)[10,20,30,40,80,60
,70]
10. Predict output ?
>>> print(list[4][1])
(a) [60]
(b)[80,60,70]
(c) [40]
(d)[10,20,30,40,80,60
,70]
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS
1. Write a program to implement the stack & queue data structure using list
2. Write a program that prints all consonants in a string using list comprehension
3. Write a program that creates a list of numbers from 1-50 that are either divisible
by 3 or divisible by 6.
Example:
>>> x=(1,2,3)
>>> print(x)
(1, 2, 3)
>>> x
(1, 2, 3)
CREATED BY K. VICTOR BABU
Operations of Tuple
• .
Some of the operations of tuple are:
• Access tuple items
• Change tuple items
• Loop through a tuple
• Count()
• Index()
• Length()
Access tuple items: Access tuple items by referring to the index number,
inside square brackets
>>> x=('a','b','c','g')
>>> print(x[1])
b
>>> print(x[3])
G
count(): Returns the number of times a specified value occurs in a tuple
>>> x=(1,2,3,4,5,6,2,10,2,11,12,2)
>>> x.count(2)
4
• Change tuple items: Once a tuple is created, you cannot change its values. Tuples are unchangeable.
• >>> x=(2,5,7,'4',8)
• >>> x[1]=10
• Traceback (most recent call last):
• File "<pyshell#41>", line 1, in <module> x[1]=10
• TypeError: 'tuple' object does not support item assignment
• >>> x
• (2, 5, 7, '4', 8) # the value is still the same
• Loop through a tuple: We can loop the values of tuple using for loop
• >>> x=4,5,6,7,2,'aa'
• >>> for i in x:
• print(i, end=“ “) Output: 4 5 6 7 2 ‘aa’
Index(): Searches the tuple for a specified value and returns the position of
where it was found
>>> x=(1,2,3,4,5,6,2,10,2,11,12,2)
>>> x.index(6)
5
Length (): To know the number of items or values present in a tuple, we use
len().
>>> x=(1,2,3,4,5,6,2,10,2,11,12,2)
>>> y=len(x)
>>> print(y)
12
LIST TUPLE
Syntax for list is slightly different Syntax for tuple is slightly different
comparing with tuple comparing with lists
List uses [ and ] (square brackets) to Tuple uses rounded brackets( and ) to
bind the elements. bind the elements.
LIST TUPLE
List can be edited once it is created in A tuple is a list which one cannot edit
python. Lists are mutable data once it is created in Python code. The
structure. tuple is an immutable data structure
1. Which data structure allows you to return multiple values from a function?
(a) List
(b) Tuple
(c) Dictionary
(d) Set
(a) true
(b) false
1. Write a Python program to get the 4th element and 4th element from last of a tuple.
• .
2. Items are accessed by their position in a dictionary and All the keys in a dictionary must be
of the same type.
(a) true
(b) false
• Write a Python code to store Roll number of student is associated with his/her name.
Read ‘n’ number of student name and roll number from user.
• Write a Python code to create phone book contacts. Read contact name and prone
number from user.
For example,
• . the following code uses a for loop to create a list of
the squares of the numbers from 0 to 9:
squares = []
for x in range(10):
squares.append(x**2)
List comprehensions can be used with other data structures such as sets,
tuples and dictionaries, but are most commonly used with lists, and can
be very useful for working with large data sets, or infinite sequences with
generator expression.
Creating a new dictionary by iterating over the key-value pairs of an existing dictionary:
• Is it possible to use the list comprehension to combine the elements of two lists.
Justify with the help of an example.
Reference Books:
1. Guido van Rossum and Fred L. Drake Jr, “An Introduction to Python – Revised and updated for
Python 3.2, Network Theory Ltd., 2011.
2. Robert Sedgewick, Kevin Wayne, Robert Dondero, “Introduction to Programming in Python: An
Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
3. Charles Dierbach, “Introduction to Computer Science using Python: A Computational Problem-
Solving Focus, Wiley India Edition, 2013.
Sites and Web links:
1. Python Notes for Professionals (bratliservice.eu)
2. BeginnersGuide - Python Wiki