UNIT 4 Py
UNIT 4 Py
LIST:
Python Lists are just like dynamically sized arrays, declared in other languages (vector in
C++ and Array List in Java). In simple language, a list is a collection of things, enclosed in [ ]
and separated by commas.
Eg:
Var = ["Geeks", "for", "Geeks"]
print(Var)
O/P
["Geeks", "for", "Geeks"]
Creating List
Lists in Python can be created by just placing the sequence inside the square brackets[].
Unlike Sets, a list doesn’t need a built-in function for its creation of a list.
Example 1: Creating a list in Python
# Creating a List
List = []
print("Blank List: ")
print(List)
Output
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
Example 2: Creating a list with multiple distinct or duplicate elements
# Creating a List with the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
Output
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
In order to access the list items refer to the index number. Use the index operator [ ] to access
an item in a list. The index must be an integer. Nested lists are accessed using nested
indexing.
Output
Output
Accessing element using negative indexing
Geeks
For
Nested Lists
In Python, nested lists are lists that contain other lists as their elements. They can be useful
for storing and manipulating complex data structures, such as matrices, graphs, or trees.
In order to create a nested list, you can simply use square brackets [] to enclose one or more
lists inside another list.
Example:
nested_list = [[1, 2, 3], ["a", "b", "c"], [True, False, None]]
print(nested_list)
Output:
[[1, 2, 3], ['a', 'b', 'c'], [True, False, None]]
Example:
nested_list = [
['puppy', 'kitty', 'petey'],
[2023, 2024, 2025],
[True, False]
]
In Python, lists are versatile data structures that come with a variety of operations and
methods to manipulate them. Here’s a rundown of common list operations and methods:
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
Tuple
A tuple is a collection similar to a Python list. The primary difference is that we cannot
modify a tuple once it is created.
Example
Output:
(1, 2, -5)
Example
# tuple including string and integer
mixed_tuple = (2, 'Hello', 'Python')
print(mixed_tuple)
Output:
(2, 'Hello', 'Python')
Tuple Characteristics
Tuples are:
Ordered - They maintain the order of elements.
Immutable - They cannot be changed after creation.
Allow duplicates - They can contain duplicate values.
Accessing Tuple
Output:
Python
C++
Updating tuple
In Python, tuples are immutable, meaning you cannot change their content once they are
created. However, you can create a new tuple that reflects the changes you want. Here are a
few ways to "update" a tuple:
1. Reassigning a Tuple
You can create a new tuple by concatenating or slicing the original tuple.
# Original tuple
original_tuple = (1, 2, 3, 4)
print(updated_tuple)
Output:
(1, 2, 99, 4)
# Original tuple
original_tuple = (1, 2, 3, 4)
print(updated_tuple)
Output:
(1, 2, 99, 4)
Deleting Tuples
We cannot delete individual items of a tuple. However, we can delete the tuple itself using
the del statement.
Example
Output:
Table Deleted
The list is better for performing operations, A Tuple data type is appropriate for
3
such as insertion and deletion. accessing the elements
Unexpected changes and errors are more Because tuples don’t change they
6
likely to occur are far less error-p
Dictionary
A Python dictionary is a collection of items, similar to lists and tuples. However, unlike lists
and tuples, each item in a dictionary is a key-value pair (consisting of a key and a value).
Create a Dictionary
We create a dictionary by placing key: value pairs inside curly brackets {}, separated by
commas. For example,
Explain
# creating a dictionary
country_capitals = {
"Germany": "Berlin",
"Canada": "Ottawa",
"England": "London"
}
print(country_capitals)
Output
{'Germany': 'Berlin', 'Canada': 'Ottawa', 'Italy': 'Rome'}
print(country_capitals)
Output
{'Canada': 'Ottawa'}
List Dictionary
The list is a collection of index value The dictionary is a hashed structure of the key and
pairs like ArrayList in Java and value pairs.
Vectors in C++.
The indices of the list are integers The keys of the dictionary can be of any
starting from 0. immutable data type.
The order of the elements entered is They are unordered in python 3.6 and below and
maintained. are ordered in python 3.7 and above.
Average time taken to search a value Average time taken to search a key in dictionary
List Dictionary
Average time to delete a certain Average time to delete a certain key from a
value from a list takes O[n]. dictionary takes O[1].