LIST INTRODUCTION
What is List ?
Introducing CRUD Operation
How to create list?
A list can be created by passing n number of elements within [] and each element separated by comma.
For ex; x = [1,2,3,4,”hii”,27]
Ways to Read Data from List
list
Indexing in python
Slicing in python
Ways to update the List Elements
Update - indexing
Update - slicing
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_list[2:5] # Output: [2, 3, 4]
my_list[1:8:2] # Output: [1, 3, 5, 7]
List Methods
APPEND
Syntax: a.append(b)
EXTEND
Adds all elements of an iterable (e.g., list) to the end of the list.
Syntax: a.extend(iterable)
INSERT
Inserts an element at a specified position in the list.
Syntax: x.insert(index, element)
How to delete list?
Delete - indexing
Delete - Slicing
How to delete list?
Pop()
remove()
clear()
del()
List method
index()
count()
count(element): Returns the number of occurrences of an element.
For Example:
x = [1, 2, 2, 3]
x.count(2)
Print(x)
Output: 2
sort()
sort(key=None, reverse=False): Sorts the elements of the list in ascending order by default
For example:
x = [3, 1, 2]
x.sort()
Output: [1, 2, 3]
x.sort(reverse=True)
Output: [3, 2, 1]
reverse()
reverse(): Reverses the elements of the list in place.
For example:
x = [1, 2, 3]
x.reverse()
Output: [3, 2, 1]
copy()
copy(): Returns a shallow copy of the list.
For example:
x = [1, 2, 3]
y = x.copy()
Output: [1, 2, 3]