Python List Function
List Functions
List data type in Python has many inbuilt functions.
Consider a list, num_list=[10,20,30,40,50]
Function |
Output |
Explanation |
num_list.append(60) |
[10,20,30,40,50,60] |
Adds an element to end of list |
num_list.index(10) |
0 |
Returns the index position of the element.
In case of multiple occurrence of the element, returns the index of the first occurrence.
Throws ValueError, if the element is not found |
num_list.insert(3,60) |
[10,20,30,60,40,50] |
Inserts an element at a given position |
num_list.pop(3) |
40 |
Removes and returns the element at given index position from the list |
num_list.remove(30) |
[10,20,40,50] |
Removes the first occurring element whose value is 30 |
num_list.sort() |
[10,20,30,40,50] |
Sorts the list in ascending order |
num_list.reverse() |
[50,40,30,20,10] |
Reverses the list |
Tryout
Tryout the below code in python playground and observe the output.
CODE/PROGRAM/EXAMPLE
crew_details={
"Pilot":"John",
"Co-pilot":"Jem",
"Head-Strewardess":"Mini",
"Stewardess":"Jerry"
}
print("Before update:")
print("Co-pilot:",crew_details.get("Co-pilot"))
crew_details.update({"Flight Attendant":"Jane", "Co-pilot":"Henry"})
print("\nAfter update:")
print("Co-pilot:",crew_details.get("Co-pilot"))
print("Flight Attendant:",crew_details["Flight Attendant"])