Python Unit. 3
Python Unit. 3
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can
store all types of items (including another list) in a list. A list may contain mixed type of items,
this is possible because a list mainly stores references at contiguous locations and actual items
maybe stored at different locations.
• List can contain duplicate items.
• List in Python are Mutable. Hence, we can modify, replace or delete the items.
• List are ordered. It maintain the order of elements based on how they are added.
• Accessing items in List can be done directly using their position (index), starting from 0
Creating a List
Here are some common methods to create a list:
Using Square Brackets
# List of integers
a = [1, 2, 3, 4, 5]
# List of strings
b = ['apple', 'banana', 'cherry']
print(a)
print(b)
print(c)
print(a)
print(b)
# Inserting 5 at index 0
a.insert(0, 5)
print("After insert(0, 5):", a)
print(a)
Removing Elements from List
We can remove elements from a list using:
• remove(): Removes the first occurrence of an element.
• pop(): Removes the element at a specific index or the last element if no index is
specified.
• del statement: Deletes an element at a specified index.
a = [10, 20, 30, 40, 50]
A list is a non-
A Tuple is a non-
homogeneous data The set data A dictionary is also a
homogeneous data
structure that stores structure is non- non-homogeneous
structure that stores
the elements in homogeneous but data structure that
elements in columns of
columns of a single stores the elements stores key-value
a single row or
row or multiple in a single row. pairs.
multiple rows.
rows.
The list can be A tuple can be nested The set can be The dictionary can
nested among all among all nested among all be nested among all
A list can be created Tuple can be created A set can be A dictionary can be
using using created using created using
the list() function the tuple() function. the set() function the dict() function.
A tuple is immutable
A list is mutable i.e A set is mutable i.e A dictionary is
i.e we can not make
we can make any we can make any mutable, its Keys are
any changes in the
changes in the list. changes in the set, not duplicated.
tuple.
List Tuple Set Dictionary
Dictionary is ordered
List is ordered Tuple is ordered Set is unordered (Python 3.7 and
above)
# updating an element
t[1] = 100
print(t)
Accessing Values in Python Tuples
Tuples in Python provide two ways by which we can access the elements of a tuple.
Python Access Tuple using a Positive Index
Using square brackets we can get the values from tuples in Python
t = (10, 5, 20)
print("Value in t[0] = ", t[0])
print("Value in t[1] = ", t[1])
print("Value in t[2] = ", t[2])
Access Tuple using Negative Index
In the above methods, we use the positive index to access the value in Python, and here we will
use the negative index within [].
t = (10, 5, 20)
print("Value in t[-1] = ", t[-1])
print("Value in t[-2] = ", t[-2])
print("Value in t[-3] = ", t[-3])
t3 = (t1, t2)
print(t3)
Output
((0, 1, 2, 3), ('python', 'java'))
Repetition Python Tuples
We can create a tuple of multiple same elements from a single element in that tuple.
t = ('python',)*3
print(t)
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a
dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and
must be immutable.
Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
find values.
d = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(d)
• Dictionary keys are case sensitive: the same name but different cases of Key will be
treated distinctly.
• Keys must be immutable: This means keys can be strings, numbers, or tuples but not
lists.
• Keys must be unique: Duplicate keys are not allowed and any duplicate key will
overwrite the previous value.
• Dictionary internally uses Hashing. Hence, operations like search, insert, delete can be
performed in Constant Time.
Accessing Dictionary Items
We can access a value from a dictionary by using the key within square brackets orget()method.
d = { "name": "Alice", 1: "Python", (1, 2): [1,2,4] }
A Set in Python is used to store a collection of items with the following properties.
• No duplicate elements. If try to insert the same item again, it overwrites previous one.
• An unordered collection. When we access all items, they are accessed without any
specific order and we cannot access items using indexes as we do in lists.
• Internally use hashing that makes set efficient for search, insert and delete operations. It
gives a major advantage over a list for problems with these operations.
• Mutable, meaning we can add or remove elements after their creation, the individual
elements within the set cannot be changed directly.
We can add and remove elements form the set with the help of the below functions –
• add(): Adds a given element to a set
• clear(): Removes all elements from the set
• discard(): Removes the element from the set
• pop(): Returns and removes a random element from the set
• remove(): Removes the element from the set
difference_update() Updates the existing caller set with the difference between two sets
intersection_update() Updates the existing caller set with the intersection of sets
issubset() Returns True if all elements of a set A are present in another set B
symmetric_difference_update() Updates the existing caller set with the symmetric difference of sets
Functions Name Description