Lists are fundamental data structures in Python and are widely used due to their flexibility and ease of use. List in python elements are written under square brackets as shown below.
List in Python
How to create an empty list in python
empty_list=[]Â Â
List of elements
a=['apple','orange','mango']
To check length of the list or how many elements are present in the list type
len(a)
Output:3
Because there are 3 elements are in the list.
To print any element in the list
a[0]
Output:'apple'
List Methods
Python lists have several built-in methods for common operations.
1. List.append:
To append element in list.
2. List.Pop:
To remove or pop element from a list by its index value.
3. List.Remove:
To remove first occurrence of a value.
4. List.Insert:
To insert object before index
5. List.Reverse:
To reverse the order of a list.
6. List.sort:
To sort a list alphabetically (A to Z) and reverse of it (Z to A).
7. List.index:
To find the index of an object
8. List.clear:
To clear element of list.
9. List.copy:
To create copy of a list.
10. List.count:
To count an element or object occurrence in list.
11. List.extend:
To extend a list.
[…] is used to insert element in list before specified index.For an instance we want to add element before 2 index in list at hen we we […]
[…] It is used for reverse the order of element in list. […]
[…] in list used to sort elements alphabetically or reverse of it as shown […]
[…] find the index or position of an object list index function is used.e.g In list a if we want to find index of object guavava it will be written […]
[…] we want to know how many times an object is present in list count feature is […]
[…] copy complete list. as shown below it simply copy list a and assign it into another list […]
[…] will return blank list as shown […]
[…] is used to Extend list by appending elements from the iterable or list.For and instance a and b are two […]
[…] shown above a has 3 element.when we extend a list element of b added at index 3,4,5.Now the length of the a a will be 6 or have 6 elements.But when […]
[…] array– contains only list of numbers and only one […]
[…] Tuple in python is written in “( )” bracket and are immutable. It can’t be modified as list. […]
[…] as chat: wchat=chat.readlines() print(wchat) print(type(wchat)) # it will give list as output. # By 'open' statement w = open('WhatsApp Chat.txt', 'r', encoding='utf-8') […]