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 :-

list = ["codeupon", 1, True]
print(list)
Python

Output :-

["codeupon", 1, True]
Markup

List Methods :

  1. len() method :- It is used to find the length of the list

Example :-

list = ["codeupon", 1, True]
print(len(list))
Python

Output :-

2
Markup

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 :-

list = ["codeupon", 1, True]
list.append("hello")
print(list)
Python

Output :-

["codeupon", 1, True, "hello"]
Markup

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 :-

list = ["codeupon", 1, True]
list.append(2,"great")
print(list)
Python

Output :-

["codeupon", 1, "great", True]
Markup

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 :-

list = ["codeupon", 1, True]
list.extend([2, "great", "ABC", "good morning"])
print(list)
Python

Output :-

["codeupon", 1, True, 2, "great", "ABC", "good morning"]
Markup

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 :-

list = ["codeupon", 1, True, 2, "great", "ABC", "good morning"]
print(list[::-1])
Python

Output :-

['good morning', 'ABC', 'great', 2, True, 1, 'codeupon']
Markup

6. remove() method :- This method is used to remove the item from the list

Example :-

list = ["codeupon", 1, True, 2, "great", "ABC", "good morning"]
list.remove(1)
list.remove(True)
print(list)
Python

Output :-

['codeupon', 2, 'great', 'ABC', 'good morning']
Markup

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 :-

list = ["codeupon", 1, True, 2, "great", "ABC", "good morning"]
# removing the last item from the list 
list.pop()
 
# removing the item by its index number 
list.pop(3)
print(list)
Python

Output :-

['codeupon', 1, True, 'great', 'ABC'] 
Markup

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

list = ["codeupon", 1, True, 2, "great", "ABC", "good morning"]
print(list[::-1])
Python

Output :-

['good morning', 'ABC', 'great', 2, True, 1, 'codeupon'] 
Markup

Example #2 :- To print the limited items from the list

list = ["codeupon", 1, True, 2, "great", "ABC", "good morning"]
print(list[0:4])
Python

Output :-

['codeupon', 1, True, 2]
Python

Creating a Nested List

List inside the List is called nested list

list1 = [1, 2, 3]
list2 = ['a','b','c']
 
my_nested_list = [list1, list2]
print(my_nested_list)
Python

Output :-

[[1, 2, 3], ['a', 'b', 'c']]
Markup

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

Popular posts from this blog

⚠️How to create a virus in termux app ?

How To Install Metasploit In Termux App ?