1
Lists In Python
In python there are following types of lists
1. Empty list
Is that type of list which has no value
Empty_list=[ ]
2. Numbers_list
Mumber list is that type of list which contains only number values like.
Number_list=[1,2,3,4,5]
3. A list of strings
Fruits=[“apple”,”banana”, “cherry”]
4. Mixed_list
A mixed list is that type of list which contains different values of different data types.
Mixed_list=[“apple”, 10,True,3.14] // True is a Boolean value (0,1)
Common List Operations and Methods:
PRINTING OR DISPLAYING WHOLE LIST
Print ( list name)
e.g Print(numbers_list)
Accessing a single element in an array
print(numbers[0]) # Output: 1
Modifying Elements: Assign a new value to an element at a specific index.
numbers[0] = 10
print(numbers) # Output: [10, 2, 3, 4, 5]
Adding Elements:
For adding elements in a list we will follow the following functions.
1. Append
2. Insert
3. Extend
1-APPENED
2
In Python, the append() method is a built-in list method used to add a single element to the end
of an existing list. It modifies the list in place, meaning it directly changes the original list rather
than returning a new one.
Here's how append() works:
Syntax: You call append() on a list object using dot notation, passing the item you want
to add as an argument within the parentheses.
List name . append ( item )
For numeric value
number=[1,2,3]
number. append (4)
print(number)
Output= [1,2,3,4]
For string values
string=["ali","waqar"]
[Link]("noor")
print(string)
Output=[’ali’,’waqar’,’noor’]
2- INSERT FUNCTION IN PYTHON
The insert() method in Python lists allows for the insertion of an element at a specified index
within the list. It modifies the list in-place, meaning it directly alters the existing list rather than
creating a new [Link] insert the new element at the specified index and move the previous
values to the next locations.
Syntax of insert function
list_name.insert(index, element)
list_name: The list on which the method is called.
index: The position (integer) where the element should be inserted.
element: The value or object to be inserted into the list.
Functionality of “INSERT” function
3
The insert() method places the element before the element currently at the
specified index.
If the index is 0, the element is inserted at the beginning of the list.
If the index is equal to or greater than the length of the list, the element is appended to
the end of the list (similar to append()).
If a negative index is provided, Python counts from the end of the list. For example,
-1 would insert before the last element, -2 before the second-to-last, and so on. If a
very large negative index is given (exceeding the list's length), the element is inserted at
the beginning.
3- EXTEND FUNCTION
The extend() method in Python is a list method used to add multiple elements from an
iterable to the end of an existing list. It effectively concatenates the elements of the iterable to
the target list, modifying the original list in place.
Here's how it works:
Takes an Iterable as Argument:
extend() requires an iterable as its argument. This can be another list, a tuple, a set, a
string, or any other object that can be iterated over (i.e., you can loop through its
elements).
Unpacks and Appends:
The method iterates through the elements of the provided iterable, one by one. For
each element in the iterable, extend() appends that individual element to the end of the
list on which the method is called.
Modifies In-Place:
extend() modifies the original list directly. It does not create a new list; instead, it
extends the existing list by adding the new elements.
No Return Value:
The extend() method returns None.
Example:
Python
my_list = [1, 2, 3]
another_list = [4, 5, 6]
4
my_list.extend(another_list)
print(my_list)
Output:
Code
[1, 2, 3, 4, 5, 6]
In this example, extend() takes another_list (which is an iterable) and adds each of its
elements (4, 5, and 6) individually to the end of my_list.
REMOVING ELEMENTS FROM THE LIST
Removing elements:
o remove(): Removes the first occurrence of a specified value.
my_list.remove("y")
o pop(): Removes and returns the element at a specified index (or the last element if no
index is given).
my_list.pop(0)
o del statement: Deletes elements by index or slices.
del my_list[1]
o clear(): Removes all elements from the list.
my_list.clear()
list1=[1,2,3]
list2=[4,5,6]
[Link]()
[Link](list2)
print(list1)
Other Common List Operations:
Length: len(list_name) returns the number of elements.
list1= [1,2,3]
length= len(list1)
5
print (length)
Slicing: Extracts a portion of the list.
Python list slicing provides a flexible way to extract sub-sequences (slices) from a list using the
syntax list[start:stop:step]. This operation creates a new list containing the selected
elements.
Parameters:
start (optional):
The index where the slice begins (inclusive). If omitted, it defaults to 0 (the beginning of
the list).
stop (optional):
The index where the slice ends (exclusive). The element at this index is not included. If
omitted, it defaults to the end of the list.
step (optional):
The increment between indices in the slice. If omitted, it defaults to 1. A
negative step value reverses the order of the slice.
How it Works:
list[start:stop]: This is the most common form. It extracts elements from start up
to, but not including, stop.
Python
my_list = [0, 1, 2, 3, 4, 5]
slice1 = my_list[1:4] # [1, 2, 3]
Omitting start or stop:
o list[:stop] starts from the beginning of the list.
o list[start:] goes to the end of the list.
o list[:] creates a shallow copy of the entire list.
Python
my_list = [0, 1, 2, 3, 4, 5]
slice2 = my_list[:3] # [0, 1, 2]
slice3 = my_list[2:] # [2, 3, 4, 5]
copy_list = my_list[:] # [0, 1, 2, 3, 4, 5] (a new list object)
Using step: This allows you to skip elements.
Python
6
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
slice4 = my_list[::2] # [0, 2, 4, 6, 8] (every other element)
Negative Indices: Negative indices count from the end of the list. -1 is the last
element, -2 is the second to last, and so on.
Python
my_list = [0, 1, 2, 3, 4, 5]
slice5 = my_list[-3:-1] # [3, 4]
Negative step: Reverses the order of the slice. When step is negative, start defaults to
the end and stop defaults to the beginning.
Python
my_list = [0, 1, 2, 3, 4, 5]
reversed_list = my_list[::-1] # [5, 4, 3, 2, 1, 0]
Key Points:
Slicing always produces a new list, not a view or reference to the original list's elements.
Slicing past the beginning or end of the list does not raise an error; it simply includes
elements up to the boundary.
Slicing can be used for extracting data, but also for modifying or deleting parts of a list
by assigning to a slice.
Concatenation: Joins two or more lists using the + operator.
In Python, concatenating lists involves combining two or more lists into a single, new
list. This operation creates a new list containing all the elements from the original lists in
the order they were concatenated.
Here are the primary ways list concatenation works in Python:
Using the + Operator: This is the most straightforward and common method. When you
use the + operator between two lists, it returns a new list that is the result of joining the
elements of the first list with the elements of the second list.
Python
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list)
# Output: [1, 2, 3, 4, 5, 6]