Lists
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.