Python [ 'L', 'I', 'S', 'T' ] 
Tags: python, list, datastructure, programming
Published: 2020-08-31 10:22:40

Whether you are a Python beginner😵 or a proficient Python Programmer/Developer🤩, one of the most common Data Structure that you will encounter is **List**📃

<br>
* List, in Python, is an *ordered, mutable, heterogeneous, dynamic* Datatype. 
* They are *ordered*, so we can access individual elements through index.
* They are *mutable*, means that the list elements can be updated.  
* A List can contain data of various datatypes like *Integer, Float, Boolean, String* etc. 
* List can contain *duplicate* elements.
* Example of a list is 
<br>
```
new_list = ["John", 2 , 2.5, True]
```
<br>

Simple✨💥......isn't it??!!!!!!😎🤩
<p>
Okay..Let's discuss some of the most common methods that we can use upon Lists.</p>
* **append()**
<p>*This method is used to add/append elements at the end of the list.*</p>

**Syntax**

```
list.append(element)
```
**Example**
```
num_list = ["John", 1, 2, 3, 4, True]
num_list.append(5)
print("Updated List: {}".format(num_list))
```
**Output**
```
Updated List: ['John', 1, 2, 3, 4, True, 5]
```
<br>
* **insert()**
<p>*This method is used to insert/add element at a specified location/index.* </p>

**Syntax**
```
list.insert(index, element)
```
**Example**
```
num_list = ["John", 1, 2, 3, 4, True]
num_list.insert(1,500)
print("Updated List: {}".format(num_list)
```
**Output**
```
Updated List: ["John", 500, 1, 2, 3, 4, True]
```
500 is inserted at index 1. 
<br>
* **count()**
<p>*This method returns the number of occurrence of a particular element in a list.*</p>


**Syntax**
```
list.count(element)
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
cnt = num_list.count(5)
print("Number of times 5 occur: {}".format(cnt))
```
**Output**
```
Number of times 5 occur: 3
```
* **sort()**
<p>*This method sorts the list in ascending order by default. To sort in descending order, we have to use <b>reverse</b> flag within the <b>sort</b> method.*</p>


**Syntax**
```
list.sort(reverse = True|False, key = function_name)
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
num_list.sort()
print("Sorted List in Ascending Order: {}".format(num_list))
num_list.sort(reverse = True)
print("Sorted List in Descending Order: {}".format(num_list))
```
**Output**
```
Sorted List in Ascending Order: [1, 2, 3, 4, 5, 5, 5, 6]                                                                      
Sorted List in Descending Order: [6, 5, 5, 5, 4, 3, 2, 1]
```

* **reverse()**
<p>*As the name implies, this method reverses a list.*</p>


**Syntax**
```
list.reverse()
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
num_list.reverse()
print("Reversed List: {}".format(num_list))
```
**Output**
```
Reversed List: [5, 6, 5, 4, 5, 3, 2, 1]
```
* **remove()**
<p>*This method removes a specified element from the list. If the specified element occurs more than once, then the first occurrence of the element is removed.*</p>


**Syntax**
```
list.remove(element)
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
print("List before updating: {}".format(num_list))
num_list.remove(5)
print("Updated List: {}".format(num_list))
```
**Output**
```
List before updating: [1, 2, 3, 5, 4, 5, 6, 5]                                                                                
Updated List: [1, 2, 3, 4, 5, 6, 5]
```
* **pop()**
<p>*This method removes the element from the specified location/index.*</p>


**Syntax**
```
list.pop(index)
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
print("List before updating: {}".format(num_list))
num_list.pop(5)
print("Updated List: {}".format(num_list))
```
**Output**
```
List before updating: [1, 2, 3, 5, 4, 5, 6, 5]                                                                                
Updated List: [1, 2, 3, 5, 4, 6, 5]
```
* **index()**
<p>*This method returns the index of a specified element. If the specified element occurs more than once, then index of the first occurrence of the element is returned.*</p>


**Syntax**
```
list.index(element)
```
**Example**
```
num_list = [1,2,3,5,4,5,6,5]
idx = num_list.index(5)
print("Index of 5: {}".format(idx))
```
**Output**
```
Index of 5: 3
```
* **extend()**
<p>*This method appends/adds the elements of any iterable (List, Set, Tuple, etc.) to the end of the current list.*</p>


**Syntax**
```
list.extend(iterable)
```
**Example**
```
num_list = [100,200,300,400,500]
char_list = ["A", "B", "C", "D"]
num_list.extend(char_list)
print("Updated List: {}".format(num_list))
```
**Output**
```
Updated List: [100, 200, 300, 400, 500, 'A', 'B', 'C', 'D']
```
* **copy()**
<p>*This method creates a copy of the specified list.*</p>


**Syntax**
```
list_2 = list_1.copy()
```
*list_2* is the copy of *list_1*
<br>
**Example**
```
num_list = [100,200,300,400,500]
new_list = num_list.copy()
print("Copy of the 'num_list': {}".format(new_list))
```
**Output**
```
Copy of the 'num_list': [100, 200, 300, 400, 500]
```
* **clear()**
<p>*This method removes all the elements from the specified list.*</p>


**Syntax**
``` 
list.clear()
```
**Example**

```
num_list = [100,200,300,400,500]
num_list.clear()
print("Cleared List: {}".format(num_list))
```
**Output**
```
Cleared List: []
```
So...working with a List isn't that difficult!!....right???!!!!😇
<br>
Happy Learning!!✌😎