Python Programming Lists Tuples Dictionary
Python Programming Lists Tuples Dictionary
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()
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.
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).
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