Py Module 3
Py Module 3
List
A list refers to a collection of objects; it represents an ordered sequence of
data. In that sense, a list is similar to a string, except a string can hold only
characters. We may access the elements contained in a list via their position
within the list. A list need not be homogeneous; that is, the elements of a list do
not all have to be of the same type.
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.
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
When we say that lists are ordered, it means that 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.
List Methods
Python has a set of built-in methods that you can use on lists.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
List Length
To determine how many items a list has, use the len() function:
Example
Print the number of items in the list:
Accessing List
Example-1
Return the third, fourth, and fifth item:
Example-2
This example returns the items from the beginning to, but NOT including,
"kiwi":
Example-3
This example returns the items from "cherry" to the end:
Example
J. JAGADEESAN, ASST. PROFESSOR OF COMPUTER SCIENCE, AAGASC, Karaikal-609605
Check if "apple" is present in the list:
Example
Append Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
Insert Items
To insert a list item at a specified index, use the insert() method.
Example
Insert an item as the second position:
Example:
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Output:
['apple', 'cherry']
Example
Sort the list numerically:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
Output
[23, 50, 65, 82, 100]
Sort Descending
To sort descending, use the keyword argument reverse = True:
Example
Sort the list descending:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
Output
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
Copy a List
You cannot copy a list simply by typing list2 = list1, because: list2 will only be
a reference to list1, and changes made in list1 will automatically also be made
in list2.
There are ways to make a copy, one way is to use the built-in List method
copy().
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
Output
['apple', 'banana', 'cherry']
Example
Append list2 into list1:
for x in list2:
list1.append(x)
print(list1)
Output
['a', 'b', 'c', 1, 2, 3]
Or we can use the extend() method, which purpose is to add elements from one
list to another list:
Example
Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Output
['a', 'b', 'c', 1, 2, 3]
Syntax
{ „Key‟: Set 1, „Key‟:Set 2,…………..,‟Key‟: Set n}
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Create a variable to store the dictionary containing the values as a set without
duplicate elements (i.e, dictionary of sets).
Example
The following program creates a dictionary of sets(without duplicates) in python
using the Naive Method −
Output
On execution, the above program will generate the following output −
{'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}}
Example
The below example shows that a python set will not allow duplicates.
{'Employ ID': {10, 11, 12, 13, 14, 15}, 'Employ Age': {33, 35, 40, 25, 28, 30}}
In the above example, we can observe that all the duplicates are removed, and
printed only the unique elements. Hence proved that a python set removes does
not allow duplicates.
Syntax
defaultdict(set)
Passing the dictionary with key and value −
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Use the defaultdict() method, to create an empty set of dictionary using the by-
passing set as an argument to it.
Give the key-value pairs for the dictionary using the [] operator.
Example
The following program creates a dictionary of sets in python using the
defaultdict() function −
defaultdict(<class 'set'>,
{'Employ ID': {10, 11, 12, 14, 15},
'Employ Age': {35, 40, 25, 28, 30}})