Python — Lists
To know about Definitions and details of data containers check out :
List:
Lists are used to store multiple items in a single variable. Lists are ordered collections of objects/elements. the list is mutable. the list can have heterogeneous data types.
Example: List_numbers = [5,40,60,58,12,32,52,97,65,68,74]
let’s Jump into the inbuilt Functions of Lists
append — syntax: <list name>.append(<object>)
extend —syntax: <list name>.append(<list of objects>)
count — syntax: <list name>.count(<object>)
copy — syntax: <New list> = <list name>.copy()
pop — syntax: <list name>.pop( *index )
remove — syntax: <list name>.remove(<object>)
reverse — syntax: <list name>.reverse()
index — syntax: <list name>.index(<object>,*s pos ,*e pos)
insert — syntax: <list name>.insert(index , <object>)
sort — syntax: <list name>.sort()
clear — syntax: <list name>.clear()
Append
Append is used to append/add the object to the end of the list.
Code: List_numbers.append(‘new element’)
the Appended list is considered a single object. but we need to add the list to the List_numbers there we use a function called extend.
Extend
Extend is used to add multiple objects to the end of the list.
Code : List_numbers.extend([56, 65, 78, 95])
Count
The count function is used to find the number of occurrences of the value/object.
Code : List_numbers.count(65)
Copy
The copy function is used to copy the Elements of the list.
Example: new_list = old_list.copy()
Pop
the pop function is used to remove an item at the specified index from the list.
Example- Code: List_numbers. pop ( 0 ) < index = 0 > , List_numbers. pop ( ) < index : default =-1 > .
Remove
Remove is used to delete a particular element/object from the list only once.
Removes only the first occurrence of value.
Example- code : List_numbers.remove(5) , List_numbers.remove(65)
Reverse
Reverse helps us to reverse the list.
Example- code: List_numbers.reverse()
Index
The index function is used to get the index of the value/object. we can also check where the Object is present in a certain range of indexes or we can skip indexes using start_position and stop search using end position.
Example- code: List_number.index(65), List_number.index(65,8,-1)
Insert
Insert is used to insert an object at a certain position, Insert takes 2 arguments those are index and Object.
Example- code : List_numbers.insert(0,5)
Sort
Sort helps to sort the list in both ascending and descending order, with the argument reverse <defult = False> that is ascending order. sort changes the original list.
Clear
Clear as the name says this function is used to clear the objects inside the list.
Example- code: List_numbers.clear()
Hi, all we have come to the end of this Paper/story. I hope this was informative.
I further discuss the List of other methods that are used in the Real-time world. Lists are the most data-container used in Python.
Stay tuned….