Operations On “List”
--
“Usage” of “Multiplication Operator” on “List”
- As with “Strings” and “Tuples”, the “Lists” support “Repetition” using the “Multiplication Operator”.
- The “Multiplication Operator” is often used for “Initializing” a “List” of a “Size, Known in Advance” with a “Constant Value” for the “Repetition”.
When “Mutable Object”, such as “List” itself, is used as the “List Element”, the “Multiplication Operator” will “Repeat” the “Reference” to the “Object” in “Each Index” of the “Original List Reference”, “Without Copying” the “Actual Object” itself in “Each Index”.
If “Any Element” from the “Inner List” is “Modified”, the “Change” can be “Reflected” through “All the Other References”, which “Refer” to the “Same Changed Object” in “Other Inner List Elements” as well.
“Search” the “Elements” in a “List”
- There are “Multiple Approaches” available to “Find” the “Elements” in a “List”.
A. “Search” the “Elements” in a “List” Using “index ()” Method
- The “index ()” Method “Takes” the “Element” to be “Searched” as the “Argument”.
- The “Elements” present in “Each Index” of the “List” are “Compared” with the “Element” to be “Searched” for “Equivalence” until the “Element” to be “Searched” is “Found”.
- The “index ()” Method “Returns” the “Index” of the “First List Element”, which is “Equal” to the “Argument” sent to the “index ()” Method.
If an “Element” is “Searched” that “Does Not Exist” in the “List” using the “index ()” Method, the “ValueError Exception” is “Thrown”.
B. “Count” the “Number of Times” an “Element” “Appears” in a “List” Using “count ()” Method
- The “count ()” Method “Takes” the “Element” to be “Searched” as the “Argument”.
- The “count ()” Method “Returns” the “Number of Times” the “Argument”, sent to the “count ()” Method, has “Appeared” in the “List”.
If an “Element” is “Searched” that “Does Not Exist” in the “List” using the “count ()” Method, “No Exception” is “Thrown”. Instead “0” is “Returned”.
C. “Membership Verification” of the “Elements” in a “List”
- “Membership” is a “Fundamental Operation” for “Lists”.
- As with “Other Collection Data Types”, the “Membership Operation” is “Performed” using the “IN” and “NOT IN” Operators on “Lists”.
“Membership Verification” of the “Elements” in a “List” Using “IN Operator”
- To “Verify” that “If” an “Element” is “Present” in a “List” using “Membership”, then “IN Operator” is used.
- If the “Element” is “Present” in the “List”, “True” is “Returned”.
- If the “Element” is “Not Present” in the “List”, “False” is “Returned”.
“Membership Verification” of the “Elements” in a “List” Using “NOT IN Operator”
- To “Verify” that “If” an “Element” is “Not Present” in a “List” using “Membership”, then “NOT IN Operator” is used.
- If the “Element” is “Not Present” in the “List”, “True” is “Returned”.
- If the “Element” is “Present” in the “List”, “False” is “Returned”.
“Remove” the “Elements” from a “List”
- There are “Multiple Approaches” available to “Remove” the “Elements” from a “List”.
A. “Remove” the “Elements” from a “List” Using the “del” Keyword
- It is possible to “Remove” an “Element” from a “List” using the “del Keyword”.
- The “del Keyword” “Takes” a “Single Parameter”, which is the “Index” of the “List Element” to be “Deleted”, and, “Removes” the “Element” from the “List”.
If the “List Index” to be “Deleted” from a “List”, using the “del Keyword”, is “Not Present” in the “List”, the “IndexError Exception” is “Thrown”.
B. “Remove” the “Elements” from a “List” Using the “remove ()” Method
- It is possible to “Remove” the “Elements” from a “List” “By Value”, rather than “By Position”, using the “remove ()” Method.
- The “remove ()” Method “Takes” a “Single Parameter”, which is the “Value” of the “List Element” to be “Deleted”, and, “Removes” the “Element” from the “List”.
If the “Value” to be “Deleted” from a “List” is “Present Multiple Times” in the “List”, the “remove ()” Method “Removes” the “First Occurrence” of the “List Element” from the “List”.
If the “Value” to be “Deleted”, using the “remove ()” Method, from a “List”, is “Not Present” in the “List”, the “ValueError Exception” is “Thrown”.
C. “Remove” the “Elements” from a “List” Using the “pop ()” Method
- It is possible to “Remove” an “Element” from a “List” by its “Index” using the “pop ()” Method.
- The “pop ()” Method “Takes” an “Optional Single Parameter”, which is the “Index” of the “List Element” to be “Deleted”, and, “Returns” the “Deleted Element”.
If “No Argument” is “Passed” to the “pop ()” Method, the “Default Index Value”, i.e., “-1” is “Passed” as an “Argument” to the “pop ()” Method “Implicitly”.
Hence, if “No Index” is provided, the “pop ()” Method “Removes” and “Returns” the “Last Item” in the “List”.
This “Helps” to “Implement” the “List” as “Stack”, i.e., “LIFO” Data Structure — “Last In First Out”.
If the “List Index” to be “Deleted” from a “List”, using the “pop ()” Method, is “Not Present” in the “List”, the “IndexError Exception” is “Thrown”.
D. “Remove” “All” the “Elements” from a “List” Using the “clear ()” Method
- It is possible to “Remove” “All” the “Elements” from a “List” using the “clear ()” Method.
“Concatenate” the “Lists”
- There are “Multiple Techniques” available to “Concatenate” the “Lists”.
- All of the “Techniques” will work with “Any Iterable Series” on the “Right-Hand Side”.
A. “Concatenation” of the “Lists” Using the “Plus Operator”
- It is possible to “Concatenate” the “Lists” using the “Plus Operator”, i.e., “+”. This “Results” in a “New List”, “Without Modification” of the “Lists” that are “Concatenated”.
B. “Concatenation” of the “Lists” Using the “Augmented Assignment Operator”
- It is possible to use the “Augmented Assignment Operator” to “Concatenate” the “Multiple Lists” in “Python”.
- In this case, the “Augmented Assignment Operator” will “Modify” the “Left List”, i.e., the “Assignee List” “In-Place”.
C. “Concatenation” of the “Lists” Using the “extend ()” Method
- It is possible to use the “extend ()” Method to “Concatenate” the “Multiple Lists” in “Python”.
- In this case, the “extend ()” Method is “Called” on the “Assignee List”, and, “Takes” the “Other List” to be “Concatenated” as the “Argument”.
- The “extend ()” Method will “Modify” the “Left List”, i.e., the “Assignee List” “In-Place”.
“Reverse” the “Elements” of a “List” “In-Place”
- The following “Technique” is available to “Reverse” the “Elements” of a “List” “In-Place”.
“Reverse” the “Elements” of a “List” “In-Place” Using the “reverse ()” Method
- A “List” can simply be “Reversed” “In-Place” by “Calling” the “reverse ()” Method on the “List”.
“Sort” the “Elements” of a “List” “In-Place”
- The following “Technique” is available to “Sort” the “Elements” of a “List” “In-Place”.
“Sort” the “Elements” of a “List” “In-Place” Using the “sort ()” Method
- A “List” can simply be “Sorted” in “Ascending Order” “In-Place” by “Calling” the “sort ()” Method on the “List”.
The “sort ()” Method “Accepts” “Two Optional Arguments”, i.e., “key” and “reverse”.
When “reverse = True” is “Passed” to the “sort ()” Method, the “List” becomes “Sorted” in “Descending Order” “In-Place”.
The “key” “Optional Argument” actually “Accepts” any “Callable Object”, which is then used to “Extract” a “Key” from “Each Item” in the “List”.
The “Items” in the “List” will then be “Sorted” according to the “Relative Ordering” of these “Keys” “In-Place”.
There are several types of “Callable Objects” in “Python”. Example — the “len ()” Function is a “Callable Object”, which is used to “Determine” the “Length” of a “Collection”, such as — “String”.
“Reverse” the “Elements” of a “List” into “Copies”
- Sometimes there are some situations where the “Reversal” of “All” the “Elements” in a “List” “In-Place” is “Not Required”.
- For the “Out-Of-Place” “Reversal” of “All” the “Elements” in a “List”, the “reversed ()” Built-In Python Method is used.
- “Calling the “reversed ()” Built-In Python Method on a “List” “Does Not Return” a “New List”. It “Returns” an “Object” of the Type “list_reverseiterator”. This “list_reverseiterator” can then be “Passed” to the “list () Constructor” to “Create” an “Actual List”.
“Sort” the “Elements” of a “List” into “Copies”
- Sometimes there are some situations where the “Sorting” of “All” the “Elements” of a “List” “In-Place” is “Not Required”.
- For the “Out-Of-Place” “Sorting” of “All” the “Elements” of a “List”, the “sorted ()” Built-In Python Method is used.
- “Calling the “sorted ()” Built-In Python Method on a “List” “Returns” a “New Sorted List”.
The “sorted ()” Built-In Python Method “Accepts” “Two Optional Arguments”, i.e., “key” and “reverse”.
When “reverse = True” is “Passed” to the “sorted ()” Built-In Python Method, it “Returns” a “New Sorted List” in “Descending Order”.
The “key” “Optional Argument” actually “Accepts” any “Callable Object”, which is then used to “Extract” a “Key” from “Each Item” in the “List”.
The “Items” in the “New Sorted List” will then be “Sorted” according to the “Relative Ordering” of these “Keys”.
There are several types of “Callable Objects” in “Python”. Example — the “len ()” Function is a “Callable Object”, which is used to “Determine” the “Length” of a “Collection”, such as — “String”.