List in Python
List are just like dynamic array, declared in other languages( like vector in c++ and array list in java ). A single list may contain datatype like ( integer, string, boolean, as well as other objects ).
List are mutable means they can be changed after it declaration or we can modify or change the list later.
List are very import part of the python programming. If you work in python most of the time you use list. So do not forget to practice more on list.
Example :-
Output :-
List Methods :–
- len() method :- It is used to find the length of the list
Example :-
Output :-
Adding elements in the list
2. using append() method :- Only one item can be added in the list at one time by using append() method. And it added the item to the end of the list.
Example :-
Output :-
3. Using insert() method :- insert() method is also like append () method but it takes two argument 1st for the position where you want to insert the data and 2nd argument takes for the item.
Example :-
Output :-
4. extend() method :- Like insert() and append() method, extend() method is also has same functionality but it can add multiple items at a time at end of the list.
Example :-
Output :-
These are the three methods that are usually used to add item in the list.
5. Negative indexing :- It means accessing the list in reverse order.
Example :-
Output :-
6. remove() method :- This method is used to remove the item from the list
Example :-
Output :-
7. pop() method :- It also same like remove() method but it remove the item from the last of the list or removing the specific item by giving the index of that item.
Example :-
Output :-
Slicing the list
To print the specific range of items from the list we use slicing
Syntax :-
list_name [start index : end index : gap/skip index]
Example #1 :- To print the list in reverse order
Output :-
Example #2 :- To print the limited items from the list
Output :-
Creating a Nested List
List inside the List is called nested list
Output :-
These are the commonly used list methods in python. I hope you understand the concept of list and its methods. If you have query and suggestion please do a comment.
Thank You.
Comments
Post a Comment