0% found this document useful (0 votes)
4 views53 pages

Python Programming Lists Tuples Dictionary

The document provides an overview of fundamental data structures in Python, including lists, tuples, and dictionaries. It explains their characteristics, methods, and advantages, highlighting how to manipulate and access data within these structures. Additionally, it covers concepts like list comprehension and dictionary operations, offering examples for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views53 pages

Python Programming Lists Tuples Dictionary

The document provides an overview of fundamental data structures in Python, including lists, tuples, and dictionaries. It explains their characteristics, methods, and advantages, highlighting how to manipulate and access data within these structures. Additionally, it covers concepts like list comprehension and dictionary operations, offering examples for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 53

Python Programming

Lists-Tuples-Dictionary

N Manjunath Gowda
Asst. Professor
Department of Mechanical Engineering
University BDT College of Engineering, Davangere.
Data Structures
Data structures are the way to store and organize the data, so that it can be accessed
effectively. Data Structures are fundamentals of any programming language around
which a program is built.
List
List
 A list is a collection of similar or different types of data.
 Lists are used to store multiple items in a single variable.
 List items are ordered, changeable(mutable), and allow duplicate values.
Accessing List items
List Methods
Method Description
append() add an item to the end of the list
extend() add items of lists and other iterables to the end of the list
insert() inserts an item at the specified index
remove() removes item present at the given index
pop() returns and removes item present at the given index
clear() removes all items from the list
index() returns the index of the first matched item
count() returns the count of the specified item in the list
sort() sort the list in ascending/descending order
reverse() reverses the item of the list
copy() returns the shallow copy of the list
len() Returns the number of elements present in the list
The append() method adds an item at the end of the list.
del()
In Python we can use the del statement to remove one or more items from a list.
copy():
Make a copy of a list with the copy() method:

list():
Another way to make a copy is to use the built-in method list()
pop()
The pop() method removes the item at the given index from the list and returns
the removed item.
count()
The count() method returns the number of times the specified element appears
in the list.
reverse()
The reverse() method reverses the elements of the list.
index()
The index() method returns the index of the specified element in the list.
Output
len()

The len() function takes a single argument/s, which can be


•sequence - string, bytes, tuple, list, range OR,
•collection - dictionary, set, frozen set

len() Return Value


len() function returns the number of items of an object.
Failing to pass an argument or passing an invalid argument will raise a TypeError excep
x = len("Hello")
Output 5
print(x)

Output
Python lists are mutable. Meaning lists are changeable. And, we can change
items of a list by assigning new values using = operator.
Python List Comprehension
List comprehension is a concise and elegant way to create lists.
A list comprehension consists of an expression followed by the for statement
inside square brackets.
Here is an example to make a list with each item being increasing by power of 2.
Example:
Square of all the numbers present in the List
Average Height Calculation
Tuple
Tuple
A tuple is a collection which is ordered and unchangeable(immutable). In Python
tuples are written with round brackets. The parentheses are optional, however,
it is a good practice to use them.
A tuple can have any number
of items and they may be of
different types (integer, float,
list, string, etc.).

Output
We can use the type() function
to know which class a variable
or a value belongs to.
Accessing Tuple items
We can access a range of items in a tuple by using the slicing operator colon :.

Output
Only count() and index() methods are available with tuple.
Advantages of Tuple over List in Python
Since tuples are quite similar to lists, both of them are used in similar situations.

However, there are certain advantages of implementing a tuple over a list:


 We generally use tuples for heterogeneous (different) data types and lists for
homogeneous (similar) data types.
 Since tuples are immutable, iterating through a tuple is faster than with a list. So there
is a slight performance boost.
 Tuples that contain immutable elements can be used as a key for a dictionary. With
lists, this is not possible.
 If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.
Dictionary
Dictionary
Python dictionary is an ordered collection of items. It stores elements in
key/value pairs. Here, keys are unique identifiers that are associated with each
value.
Dictionaries are written with curly brackets

If we want to store information about countries and their capitals, we can create
a dictionary with country names as keys and capitals as values.

Keys Values
Nepal Kathmandu
Italy Rome
England London
Example:

Output

We can add elements to a dictionary using the name of the dictionary with [].

Output
We can also use [] to change the value associated with a particular key.

Output
We use the keys to access their corresponding values.
We use the del statement to remove an element from the dictionary.

Output
Function Description
Return True if all keys of the dictionary are True (or if the dictionary is
all()
empty).

Return True if any key of the dictionary is true. If the dictionary is


any()
empty, return False.

len() Return the length (the number of items) in the dictionary.

sorted() Return a new sorted list of keys in the dictionary.

clear() Removes all items from the dictionary.

keys() Returns a new object of the dictionary's keys.

values() Returns a new object of the dictionary's values


Items in dictionaries are unordered
Program to print birthday of friends
Output
Python Dictionary Comprehension
Checking Whether a Key or Value Exists in a Dictionary
in and not in operators
get() method to retrieve key value
get() method that takes two arguments: the key of the value to retrieve and a
fallback value to return if that key does not exist.

Without get() method


setdefault() method
Write a Python program to add a key to a dictionary.

Sample Dictionary : {0: 10, 1: 20}


Expected Result : {0: 10, 1: 20, 2: 30}

Output
Write a Python script to concatenate following
dictionaries to create a new one.
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5:
50, 6: 60}

Output

You might also like