0% found this document useful (0 votes)
16 views

Lists

Uploaded by

sudaiskahan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lists

Uploaded by

sudaiskahan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

LISTS

The list is a sequence data type which is used to store the collection of
items enclosed in squared brackets [] and separated by commas.
Examples:
# Empty list
my_list=[ ]
#List of integers
Numbers=[2,3,4,5,6]
#List of strings
Words=[“apple”, ”banana”, ”cherry” ]
#Mixed data type
mixed= [1, 2.5, “apple”, [3, 4,5,8]]
• Features of lists
Mutable: Lists can be modified after creation, allowing you to add, remove, or
change elements.
Ordered: The order of elements is preserved, meaning you can access elements
by their position.
Dynamic sizing: You can add or remove elements without specifying a fixed
size.
Heterogeneous: Lists can contain elements of different data types e.g. strings,
integers, floats.
Allow duplicates: s can contain duplicate values.

Creating a list
Lists are created by placing items (elements) inside the squared
brackets and separated by commas.
E.g. my_list = [1, 2, 3, “apple”, “banana”]
Print(my_list) #output: [1,2,3,”apple”,”banana”]
Counting elements in a list.
The count() is used to count how many times a specific element appears in a
list.
Example:
my_list=[1, 2, 2, 3, 3, 3, 3, 4, 5]
print(my_list.count(3))
#output: 4
• List length
The len() is used to get the length of the list.
Example:
my_list=[1, 2, 2, 3, 3, 3, 3, 4, 5]
print(len(my_list))
#output: 9
Accessing elements from list
Elements can be accessed using positive or negative indexing.
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.
Example.
# Accessing an element from the list.
numbers= [10, 20, 30, 40, 50, 60]
Print(numbers[0]) #output: 10
Print(numbers[-1]) #output: 60
List slicing:
Slicing allows you to extract a part of the list by specifying a start and
end index.

Example:
numbers= [10, 20, 30, 40, 50, 60]
# Extracting from index 1 to 3
Print(numbers[1:4]) #output: [20, 30, 40]
# Extracting from the start to index 2
Print(numbers[:3]) #output: [10, 20, 30]
# Extracting from index 2 to the end
Print(numbers[2:]) #output: [30, 40, 50, 60]
Modifying lists:
Since lists are mutable, you can change their elements by adding new
elements, removing existing ones or substituting the elements
Adding elements to a python list.
Elements can be added to a list using the append(), insert(), or extend()
methods.
Method 1: Using append()
Elements can be added to the List by using the built in append() function.
Only one element at a time can be added to the list by using the append()
method.
Tuples can also be added to the list with the use of the append method
because tuples are immutable.
syntax:
list.append(element)
• element : an item that will be added to the end of the list.
Examples

numbers=[10,20,30,40,50,60]
# Addition of elements in the list.
numbers.append(1)
print(numbers) #output: [10, 20, 30, 40, 50, 60, 1]
numbers.append(2)
numbers.append(4)
print(“The list after addition of three elements:”)
print(numbers)
#output:
The list after addition of three elements:
[10, 20, 30, 40,50, 60, 1, 2, 4]
Method 2: Using insert()
The insert() function is used to insert an element at a specified index in a list.
Syntax:
list.insert(index,element)
• index: the position where the element should be inserted
• element:the element you want to insert into the list.
Example:
numbers=[10, 20, 30, 40, 50, 60]
#Inserting 90 at index 2
numbers.insert(2, 90)
Print(numbers)
#output: [10, 20, 90, 30, 40, 50, 60, ]
Method 3: Using extend()
The extend() function is used to add the elements of an iterable like another
list, tuple, string to the end of the current list.
Unlike append() which adds an entire object as a single element, extend()
adds each element of the iterable individually.
Syntax:
List.extend(iterable)
• Iterable: this can be another list, string or tuple
Example:
numbers=[10, 20, 30, 40, 50, 60]
# Adding each element of the iterable to the list
numbers.extend([1, 2, 3,4])
print(numbers)
#output: [10, 20, 30, 40, 50, 60, 1, 2, 3, 4]
Removing existing elements from the list.
Elements can be removed from a list using the remove() or pop()
methods.

Method 1 : using remove()


The remove() function is used to remove the first occurrence of a
specified element from a list.
If the element is not found, it raises a ValueError
syntax:
list.remove(element)
• element: the item you can to remove from the list.
Example
numbers=[10,50,30,40,50,60]
#Removing the first occurrences of 50
numbers.remove(50)
Print(numbers)
#output: [10, 30, 40, 50, 60]
#Removing an element that is not in the list
numbers.remove(6)
print(numbers)
#output: Raises ValueError
Method 2 : using pop()
The pop () function is used to remove and return an element from the
list, but by default it removes only the last element of the list.
To remove an element from a specific position of the List, the index of
the element is passed as an argument to the pop() method.
Syntax:
List.pop(index)
• index (optional): the position of the element you want to remove. If
not provided, the last element is removed.
Examples

numbers=[10, 20, 30,40,50,60]


#Removing the last element from the list
numbers.pop()
print (numbers)
#output: [10, 20, 30, 40, 50]
#Removing an element at index 2
numbers.pop(2)
print(numbers)
#output: [10, 20, 40, 50]
Substituting the elements in the list
Syntax:
list[index]= new element
• index: the position of the element to be substituted
• new element: the new substitute
Example:
numbers=[10,20,30,40,50,60]
numbers[2]=99
Print(numbers)
#output: [10, 20, 99, 40, 50, 60]
Reversing a List in Python
1. Using the reverse() method
The reverse() function reverses a list in place, meaning it modifies the
original list.
Syntax:
list.reverse()
Examples:
integers=[1, 2, 3, 4, 5]
#Reversing the list
integers.reverse()
print(integers)
#output: [5, 4, 3, 2, 1]
2.Using list slicing
You can reverse a list using slicing with the step -1.
Note: This creates a new list and does not modify the original list.
Examples:
integers=[1, 2, 3, 4, 5]
#Reversing by creating a new list
reversed_list = integers[::-1]
print(reversed_list]
#output: [5, 4, 3, 2, 1]
Why are lists used in programming.

• Lists provides easy access to elements through indexing. You can


retrieve or modify elements using their index which simplifies tasks
such as data manipulation, searching or sorting.
• Lists can hold elements of different data types (integers, strings, floats
or even other lists).
• Lists are used in programming because they are suitable for many
different kinds of applications such as:
i. Storing and manipulating data in web applications e.g. user inputs or forms.
ii. Managing real-time data in games e.g. player scores or games events.
iii. Storing records or transactions in databases or finance applications.

You might also like