How to declare list in python?
A list is a data structure, or it can be considered a container that can be used to store multiple data at once. The list will be ordered and there will be a definite count of it. The elements are indexed according to a sequence and indexing is done with 0 as the first index.
To create a list you separate the elements with a comma and enclose them with a bracket”[]”.
e.g.,
a=["Python", "C++", "C", "Java"] print(a)
We can create a two dimensional list. This is done by nesting a list inside another list.
e.g.,
a=[['1', '2'], ['3', '4']] print(a)
Some basic function to modify list are:-
- list.append() – This function will add another element to the list at the end.
syntax:-
lst=[] lst.append("name_of_element")
- list.insert() – This will add another element to the list at the given index, shifting the elements greater than the index one step to the right.
syntax:-
lst=[] lst.insert(index_number, "Element_name")
- list.pop() – This function will remove the last element from the list. If the index is provided, then it will remove the element at the particular index.
syntax:-
lst=[] lst.pop(index_number)
This is all about lists in python.
Subscribe
Login
Please login to comment
0 Discussion