LIST IN PYTHON
What is a List?
A list in Python is a collection used to store multiple items in one variable.
Lists are:
o Ordered
o Mutable → you can change, add, or remove items after the list is created.
o Heterogeneous → they can store different types of data in the same list.
o Allow duplicate values.
o Lists are created using square brackets
Create a List:
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.
Indexing in Python Lists
Python uses zero-based indexing, which means:
Positive Indexing
Positive indexing starts from 0 and moves from left to right.
The first element has index 0, the next is 1, and so on till n-1, where n is the total
number of elements in the list.
Used when you want to access items from the beginning of the list.
Example
Negative Indexing
Negative indexing starts from -1 and moves from right to left.
The last element has index -1, the previous is -2, etc.
Used when you want to access items from the end of the list.
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.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after it has
been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Accessing List Items : Python uses zero-based indexing.
What Is List Slicing?
Slicing means dividing the list into various sub lists. If you want to access a range of elements
from a list, you need to slice it. You can do this by suing the simple slicing operator i.e., colon(:).
With this operator, you can specify where to start the slicing, where to end, and specify the step.
Consider the following example and observe the elements stored in memory.
Negative Index -6 -5 -4 -3 -2 -1
“apple” “banana” “orange” “apricot” “Kiwi” “Grapes”
Positive Index 0 1 2 3 4 5
Consider the following examples to understand how a sublist can be retrieved from a list using
the slicing operations
[Link] Indexing and Slicing in List Output
1 print(fruits[2]) orange
2 print(fruits[-2]) Kiwi
3 print(fruits[1:4]) [‘banana’,’orange’,’apricot’]
4 print(fruits[0:2]) [‘apple’,’banana’] Slicing in list
5 print(fruits[-3:-1]) [‘apricot’,’Kiwi’]
6 print(fruits[-3::]) [‘apricot’,’Kiwi’,’grapes’]
7 fruits[-5:-2]==fruits[1:4] True
8 fruits[-1:-2]==fruits[1:4] False
Concatenation : It means to append the elements of one list to another . Lists allow you to add
duplicate values. The most common and easiest way to join the list is by using the plus(+)
operator.
Examples
List Built –in Functions
1. len( ) : is used to get the number of items in an object. It is most commonly used with
strings, lists, tuples, dictionaries.
Example
2. sum( ): The sum() function adds the elements of the sequence and returns the sum.
3. Max( ) : This function returns the maximum value present in the list. It takes the list as a
parameter.
4. Min( ): This function returns the minimum value present in the list. It takes the list as a
parameter.
METHODS
1. append( ): append() method in python is used to add a single item to the end of list.
This method modifies the original list and does not return a
new list.
2. insert( ) : It allows you to add an element at a specified position in the list.
Syntax of insert()
list_name.insert(index, element)
index — the position (integer) where you want the new element to go. Lists in
Python start with index 0
element — the item/value you want to add (can be a number, string, another
list, etc.)
3. reverse(): It is used to reverse the elements of the list. This function does not return any
value. It reverse the sequence of the elements in the list and updates the original list.
4. pop( ): The pop() function removes the element at the given index from the list and
returns the removed item. It takes single index as an argument.
5. clear( ): Remove all elements from the list.
Output
6. remove( ): The remove() method removes the first occurrence of a duplicate item or
matching element that has been provided as the functions arguments from the list
7. Count( ): The count() function returns the total number of elements with the specified
value provided in argument.
Python List Practice Programs (10 Questions)
1. Sum of Elements
numbers = [5, 10, 15, 20, 25]
total = sum(numbers)
print("Sum =", total)
2. Largest and Smallest Number
nums = [12, 45, 2, 67, 33]
print("Largest =", max(nums))
print("Smallest =", min(nums))
3. Count Occurrence of an Element
lst = [1, 2, 2, 3, 4, 2, 5]
n = int(input("Enter number to count: "))
print("Count =", [Link](n))
4. Reverse a List (Without using reverse())
lst = [10, 20, 30, 40]
rev = lst[::-1]
print("Reversed List =", rev)
5. Print Even Numbers from a List
lst = [11, 22, 35, 40, 55, 60]
for i in lst:
if i % 2 == 0:
print(i)
6. Add an Element to the End of List
lst = [1, 2, 3]
n = int(input("Enter a number: "))
[Link](n)
print(lst)
7. Insert an Element at a Specific Position
lst = [10, 20, 40, 50]
[Link](2, 30)
print(lst)
8. Delete an Element from List
lst = [5, 10, 15, 20]
n = int(input("Enter number to remove: "))
[Link](n)
print(lst)
9. Find the Square of Each Element
lst = [2, 4, 6, 8]
for i in lst:
print(i*i)
10. Check if an Element Exists in a List
lst = [15, 25, 35, 45, 55]
n = int(input("Enter number to search: "))
if n in lst:
print("Found")
else:
print("Not Found")