0% found this document useful (0 votes)
9 views69 pages

Unit-4-List Tuple Dictonary

This document provides an overview of lists in Python, including their properties, methods, and functions such as indexing, slicing, and mutability. It covers the creation of lists, operations like appending and sorting, and introduces multidimensional lists. Additionally, it includes self-assessment questions to reinforce understanding of the material presented.

Uploaded by

sonaljainmaths
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views69 pages

Unit-4-List Tuple Dictonary

This document provides an overview of lists in Python, including their properties, methods, and functions such as indexing, slicing, and mutability. It covers the creation of lists, operations like appending and sorting, and introduces multidimensional lists. Additionally, it includes self-assessment questions to reinforce understanding of the material presented.

Uploaded by

sonaljainmaths
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

Unit – 4

LISTS, TUPLES, DICTIONARIES


By
Dr. Ravi Gorripati

CREATED BY K. VICTOR BABU


INTRODUCTION
to

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

• Lists in Python are similar to arrays in C or Java.


• A list represents a group of elements.
• The main difference between a list and an array is that a list can store different
types of elements, but an array can store only one type of elements.
• Also, lists can grow dynamically in memory (Mutable). But the size of arrays is
fixed and they cannot grow at runtime.
• Lists are represented using square brackets []
• The elements are written in [], separated by commas
• Example:
• fruits = [‘apple’, ’banana’, ’orange’]
• Num = [10,5,30]

CREATED BY K. VICTOR BABU


Lists and Definite Loops

CREATED BY K. VICTOR BABU


Looking Inside Lists

• .

CREATED BY K. VICTOR BABU


"Lists are Mutable"

• Strings are “immutable” - we


cannot change the contents of
a string - we must make a new
string to make any change
• Lists are “mutable” - we can
change an element of a list
using the index operator

CREATED BY K. VICTOR BABU


How Long is a List?

• The len() function takes a list as


a parameter and returns the
number of elements in the list

• Actually len() tells us the number


of elements of any set or
sequence (such as a string...)

CREATED BY K. VICTOR BABU


Using the range Function

• The range function


returns a list of numbers
that range from zero to
one less than the
parameter

• We can construct an index


loop using for and an
integer iterator

CREATED BY K. VICTOR BABU


Concatenating Lists Using +

• We can create a new list


by adding two existing
lists together

CREATED BY K. VICTOR BABU


List Methods

CREATED BY K. VICTOR BABU


Lists: Common
Methods
• L.append() : Adds one item to the end of the
list.
• L.extend() : Adds multiple items to the end of
the list.
• L.pop(i) : Remove item ‘i’ from the list.
Default:Last.
• L.reverse() : Reverse the order of items in list.
• L.insert(i,item): Inserts ‘item’ at position i.
• L.remove(item) : Finds ‘item’ in list and deletes
it from the list.
• L.sort(): Sorts the list in- place i.e.,
CREATED BY K. VICTOR BABU changes the sequence in the list.
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana’]
>>> fruits.count('apple’)
>>> 2
>>> fruits.count('tangerine’)
>>>0
>>> fruits.index('banana’)
>>> 3
>>> fruits.index('banana', 4) # Find next banana starting at position 4
>>> 6
>>> fruits.reverse()
>>> fruits
>>> ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange’]
>>> fruits.append('grape’)
>>> fruits
>>> ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape’]
>>> fruits.sort()
>>> fruits
>>> ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear’]
>>> fruits.pop()
>>> 'pear'

CREATED BY K. VICTOR BABU


Built-in Functions and Lists

• There are a number of


functions built into Python
that take lists as parameters

• Remember the loops we


built? These are much
simpler.

CREATED BY K. VICTOR BABU


Lists Can Be Sliced Using :

Remember: Just like


in strings, the second
number is “up to but not
including”

CREATED BY K. VICTOR BABU


Strings and 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

# Cloning (copying) first list to second list


import copy

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]

CREATED BY K. VICTOR BABU


Cloning or Copying a list

# Python code to clone or copy a list


# Using list comprehension
def Cloning(lst1):
li_copy = [i for i in lst1]
return li_copy

# 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

CREATED BY K. VICTOR BABU


MultiDimensional
Lists
• Nested Lists need not be homogeneous.
• my2DList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,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

CREATED BY K. VICTOR BABU


Arbitrary dimensional
• NestedLists
Lists need not be of the same length.
• my2DList = [[1,2,3,’a’],[5,6,7],[9,10,’e’,12,’cat’],[’beta’,14,15,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

CREATED BY K. VICTOR BABU


cat
Arbitrary dimensional
Lists
• Nested Lists can have arbitrary depth as well.
• subL = [[’p’,’q’],[‘r’,’s’]]
• my2DList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,subL]]
0 1 2 3
my2DLis
t

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

CREATED BY K. VICTOR BABU


Lists as sequences of references

• myList = [’Name’,[Month,Date,Year],Address,
[Home,Cell]]
Str [Ref] Str [Ref]

int Str

int Str

int

CREATED BY K. VICTOR BABU


Lists as sequences of references

• myList = [’Name’,[Month,Date,Year],Address,
[Home,Cell]]
[Ref] [Ref] [Ref] [Ref]

Str int Str


Str

int Str

int

CREATED BY K. VICTOR BABU


Lists are
>>>subL = [[’p’,’q’],[‘r’,’s’]]
mutable!!
>>>myList = [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,subL]]
>>>myList [[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’q’],[‘r’,’s’]]]]
>>>subL[0][1] = ‘z’
>>>myList

[[1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,[[‘p’,’z’],[‘r’,’s’]]]]

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

• Is it possible to use the list comprehension to combine the elements


of two lists. Justify with the help of an example.
• What is slice operation in lists? Explain with suitable example.
• Discuss the different ways in which you can create a list
• Explore the various builtin functions and methods associated with
list
• Difference between list, tuple and string functions

CREATED BY K. VICTOR BABU


SUMMARY

This Session discussed about …


• Concept of a collection
• Lists and definite loops
• Indexing and lookup
• List mutability
• Functions: len, min, max, sum
• Slicing lists
• List methods: append, remove
• Sorting lists
• Splitting strings into lists of words
• Using split to parse strings

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Which slice operation will reverse the list?

(a) numbers[-1::]
(b) numbers[::-1]
(c) numbers[:-1:]
(d) numbers[9:8:1]

2. If List =[1,2,3,4,5,6,7,8,9,10], then print List[8:4:-1]

(a) [2,3,4,5]
(b) [9,8,7,6]
(c) [5,4,3,2]
(d) [6,7,8,9]

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

3. What is the output ?


>>> [0] * 4

(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]

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS
5. What is the output ?
>>> t = ['a', 'b', 'c', 'd', 'e', 'f’]
>>>
(a) ['a', 'b', 'c', t[1:3]
'd', 'e',= ['x', 'y’]
>>> t
'f’]
(b)[‘x’, ‘y', 'c', 'd', 'e',
'f’]
(c) ['a', 'x', 'y', 'd', 'e',
'f']
(d)['a', ‘b', ‘x', ‘y', 'e',
6. Predict output ?
'f'] >>> [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]

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS
7. What is the output ?
>>> numbers = [n**2 for n in range(1, 6)]
>>> print (numbers)

(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) ['a', 'b', ‘c’, ‘d’, ‘e’]


(b) ['a', 'b', ‘c’][‘d’, ‘e’]
(c) [a, b, c, d, e]
(d) ['d', 'e']
CREATED BY K. VICTOR BABU
SELF-ASSESSMENT QUESTIONS
9. What is the output ?
>>> list = [10, 20, 30, 40, [80, 60, 70]]
>>> print(list[4])

(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.

CREATED BY K. VICTOR BABU


Tuples
This Session will discuss about …
• Tuple and their purpose
• Creating tuple
• Operations on a tuple
• Methods used in tuple

CREATED BY K. VICTOR BABU


Python Tuples

• Tuple is an immutable sequence of elements that allows the individual


items to be of different types.
• Equivalent to lists, except that they can’t be changed.
• Tuples are written with round brackets ( ).
• Supports all operations for sequences.
• Tuples are more efficient than list due to python’s implementation.
• If the contents of a list shouldn’t change, use a tuple to prevent items
from accidently being added, changed, or deleted.

CREATED BY K. VICTOR BABU


Creating a Tuple

• We can construct tuple in many ways:

• X=() #no item tuple


• X=(1,2,3)
• X=tuple(list1)
• X=1,2,3,4

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()

CREATED BY K. VICTOR BABU


Operations of Tuple

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

CREATED BY K. VICTOR BABU


Operations of Tuple

• 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’

CREATED BY K. VICTOR BABU


Python – Tuple Operations

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

CREATED BY K. VICTOR BABU


Python – Removing a Tuple

• You cannot remove or delete or update items in a tuple.


• Tuples are unchangeable, so you cannot remove items from it, but you can
delete the tuple completely:

NOTE: TUPLES ARE IMMUTABLE

CREATED BY K. VICTOR BABU


List vs Tuple

LIST TUPLE
Syntax for list is slightly different Syntax for tuple is slightly different
comparing with tuple comparing with lists

Weekdays=[‘Sun’,’Mon’, ‘wed’,46,67] twdays = (‘Sun’, ‘mon', ‘tue', 634)


type(Weekdays) type(twdays)
class<‘lists’> class<‘tuple’>

List uses [ and ] (square brackets) to Tuple uses rounded brackets( and ) to
bind the elements. bind the elements.

CREATED BY K. VICTOR BABU


List vs Tuple

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

More methods or functions are Compared to lists, tuples have Less


associated with lists. methods or functions.

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

1. Write a Python program to convert a tuple to a string.


2. How tuple is different from List and Set
3. How are tuples a useful data structure?

CREATED BY K. VICTOR BABU


SUMMARY

This Session discussed about …


1. Understand the need of Tuples
2. Solve problems by using Tuples
3. Tuple functions

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Which data structure allows you to return multiple values from a function?

(a) List
(b) Tuple
(c) Dictionary
(d) Set

2. Lists are faster than tuples

(a) true
(b) false

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. Write a Python program to get the 4th element and 4th element from last of a tuple.

2. Write a Python program to find the repeated items of a tuple.

3. Write a Python program to check whether an element exists within a tuple.

CREATED BY K. VICTOR BABU


Dictionary
This Session will discuss about …
• Dictionary Data Structure and its characteristics
• Creating, copying, removing and traversal operation on Dictionary
• Methods used in Dictionary

CREATED BY K. VICTOR BABU


What is Dictionary

• Dictionaries are unordered collections of objects, optimized for quick


searching.
• Instead of an index, objects are identified by their ‘key’.
• Each item within a dictionary is a ‘key’:’value’ pair.
• Equivalent to hashes or associative arrays in other languages.
• Like lists and tuples, they can be variable-length, heterogeneous and of
arbitrary depth.
• ‘Keys’ are mapped to memory locations by a hash function

CREATED BY K. VICTOR BABU


Python Dictionary

CREATED BY K. VICTOR BABU


Dictionaries are Collection but not Sequence

• .

CREATED BY K. VICTOR BABU


Dictionaries are Mutable

CREATED BY K. VICTOR BABU


Dictionaries are Iterable

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

• Write a function called letterCount that:


• Takes in a string as a parameter
• prints a table of the letters of the alphabets (in alphabetical order) together
with the number of times each letter occurs
• Case should be ignored

• Compare Dictionary Data Structure with other data structures

CREATED BY K. VICTOR BABU


SUMMARY

This Session discussed about …


• Dictionary Data Structure and its characteristics
• Creating, copying, removing and traversal operation on Dictionary
• Methods used in Dictionary

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Which of the following are true of Python dictionaries:


(a) Dictionaries can be nested to any depth.
(b) Dictionaries are accessed by key.
(c) All the keys in a dictionary must be of the same type.
(d) Dictionaries are mutable.

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

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

• 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.

• Write a Python program to map two lists into a dictionary.



• Write a Python program to combine two dictionary adding values for common keys.
• d1 = {'a': 100, 'b': 200, 'c':300}
• d2 = {'a': 300, 'b': 200, 'd':400}
• Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})

• Write a Python script to sort (ascending and descending) a dictionary by value

CREATED BY K. VICTOR BABU


INTRODUCTION
To
LIST COMPREHENSION

This Session will discuss about …


• List Comprehension Syntax
• List Comprehension with Condition
• use list comprehensions to perform operations on the
elements of a list, such as mapping or flattening.
• To use list comprehensions to create nested lists.
• Understand how to use list comprehensions with other data
structures, such as sets, tuples and dictionaries

CREATED BY K. VICTOR BABU


List Comprehension

• List comprehensions in Python are a concise and efficient way to


create a new list based on an existing iterable.
• They are a more readable and concise alternative to traditional loops,
such as for loops, and can be used to perform operations on the
elements of a list, such as filtering, mapping, or flattening.

CREATED BY K. VICTOR BABU


List Comprehension Syntax

The basic syntax of a list comprehension is as follows:

new_list = [expression for item in iterable]

where expression is the operation to be performed on


each item in iterable, and new_list is the resulting list.

CREATED BY K. VICTOR BABU


List Comprehension Example

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)

The same result can be achieved using a list comprehension as


follows:
squares = [x**2 for x in range(10)]

CREATED BY K. VICTOR BABU


"List Comprehension with filtering

List comprehensions can also include an if clause to filter


elements from the iterable, for example:

even_squares = [x**2 for x in range(10) if x % 2 == 0]

CREATED BY K. VICTOR BABU


Nested List Comprehension

List comprehensions can also be nested, for example:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

flat = [num for row in matrix for num in row]

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.

CREATED BY K. VICTOR BABU


Use list comprehensions with other data structures

Creating a set using a list comprehension:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


even_numbers_set = {x for x in numbers if x % 2 == 0}
print(even_numbers_set)

Output: {2, 4, 6, 8, 10}

Creating a tuple using a list comprehension:


words = ['hello', 'world', 'python', 'programming']
lengths_tuple = tuple(len(x) for x in words)
print(lengths_tuple)

Output: (5, 5, 6, 11)


CREATED BY K. VICTOR BABU
Use list comprehensions with other data structures

Creating a dictionary using a list comprehension:

names = ['Alice', 'Bob', 'Charlie', 'David']


age = [25, 30, 35, 40]
name_age_dict = {names[i]: age[i] for i in range(len(names))}
print(name_age_dict)
Output: {'Alice': 25, 'Bob': 30, 'Charlie': 35, 'David': 40}

Creating a new dictionary by iterating over the key-value pairs of an existing dictionary:

original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}


new_dict = {k: v*2 for k, v in original_dict.items()}
print(new_dict)

Output: {'a': 2, 'b': 4, 'c': 6, 'd': 8}


CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

• Is it possible to use the list comprehension to combine the elements of two lists.
Justify with the help of an example.

CREATED BY K. VICTOR BABU


SUMMARY

This Session discussed about …


• List Comprehension Syntax
• List Comprehension with Condition
• use list comprehensions to perform operations on the elements of a list,
such as mapping or flattening.
• To use list comprehensions to create nested lists.
• Understand how to use list comprehensions with other data structures,
such as sets, tuples and dictionaries

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS
1. What is a list comprehension and what is it used for?
2. How do you create a set using a list comprehension?
3. How do you create a tuple using a list comprehension?
4. How do you create a dictionary using a list comprehension?
5. Can you give an example of using a list comprehension to create a new dictionary by
iterating over the key-value pairs of an existing dictionary?
6. What are some of the benefits of using list comprehensions over traditional for
loops?
7. How do you ensure that list comprehensions are readable and maintainable in your
code?
8. What are some of the trade-offs to consider when using list comprehensions?
9. Can you give an example of a situation where a list comprehension may not be the
most appropriate choice?
10. How can you use list comprehensions to filter and transform lists of data in a single
line of code?
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS
1. Using a list comprehension, create a new list that contains only the even numbers from an
existing list of integers.
2. Using a list comprehension, create a new list that contains the squares of all the numbers in an
existing list.
3. Using a list comprehension, create a new set that contains the unique words from an existing
list of strings.
4. Using a list comprehension, create a new dictionary that contains the frequency of each word in
an existing list of strings.
5. Using a list comprehension, create a new list that contains only the elements that are common
to two existing lists.
6. Using a list comprehension, create a new list that contains the uppercase versions of all the
elements in an existing list of strings.
7. Using a list comprehension, create a new tuple that contains the elements of an existing list in
reverse order.
8. Using a list comprehension, create a new list that contains the elements of an existing list, but
with duplicates removed.
9. Using a list comprehension, create a new dictionary that maps the elements of an existing list
to their corresponding indices.
10. Using a list comprehension, create a new list that contains the Cartesian product of two existing
lists.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

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

CREATED BY K. VICTOR BABU

You might also like