Operations On “List”

Oindrila Chakraborty
8 min readAug 11, 2023

“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”.
“Initialize” a “List” of “Single Element” Using “Multiplication Operator”
“Initialize” a “List” of “Multiple Elements” Using “Multiplication Operator”

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.

“Modify” the “Third Nested List Element”. “Change” Will “Reflect” in “Others”

“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.
“Find” the “First Occurrence” of an “Element” in a “List” Using the “index ()” Method

If an “Element” is “Searched” that “Does Not Exist” in the “List” using the “index ()” Method, the “ValueError Exception” is “Thrown”.

“Find” an “Element” That “Does Not Exist” in a “List” Using the “index ()” Method — “ValueError”

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”.
“Find” the “Number of Times” an “Element” is “Present” in the “List” Using the “count ()” Method

If an “Element” is “Searched” that “Does Not Exist” in the “List” using the “count ()” Method, “No Exception” is “Thrown”. Instead “0” is “Returned”.

“Find” the “Number of Times” an “Element” is “Present” in the “List”, which “Does Not Exist” in the “List”, Using the “count ()” Method

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”.
“Verify” If an “Element” is “Present” in a “List” Using the “IN Operator”

“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”.
“Verify” If an “Element” is “Not Present” in a “List” Using the “NOT IN Operator”

“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”.
“Remove” the “Third Element”, Present at “Index = 2”, from the “List” Using the “del Keyword”

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”.

“Remove” the “Sixteenth Element”, Present at “Index = 15”, from the “List” Using the “del Keyword”

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”.
“Remove” the “Element”, i.e., “Pomengrate”, from the “List” Using the “remove ()” Method

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”.

“Remove” the “Element”, i.e., “Apple”, Which is “Present Multiple Times”, from the “List” Using the “remove ()” Method

If the “Value” to be “Deleted”, using the “remove ()” Method, from a “List”, is “Not Present” in the “List”, the “ValueError Exception” is “Thrown”.

“Remove” the “Element”, i.e., “Papaya”, Which is “Not Present”, in the “List” Using the “remove ()” Method

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”.
“Remove” the “Third Element”, Present at “Index = 2”, from the “List” Using the “pop ()” Method

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”.

“Remove” the “Last Element” from the “List” Using the “pop ()” Method “Without Providing” Any “Index”

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”.

“Remove” the “Fifteenth Element”, Present at “Index = 14”, from the “List” Using the “pop ()” Method

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.
“Remove” “All” the “Elements” from the “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”.
“Concatenate” “Multiple Lists” Using the “Plus Operator”, i.e., “+”

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”.
“Concatenate” “Multiple Lists” Using the “Augmented Assignment Operator”

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”.
“Concatenate” “Multiple Lists” Using the “extend ()” Method

“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”.
“Reverse” the “Elements” of a “List” “In-Place” Using the “reverse ()” Method

“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”.
“Sort” the “Elements” of a “List” in “Ascending Order” “In-Place” Using the “sort ()” Method

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”.

“Sort” the “Elements” of a “List” in “Descending Order” “In-Place” Using the “sort ()” Method

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”.

“Sort” the “Elements” of a “String List” by the “Length of Its Elements” Using the “sort ()” Method

“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”.
“Reverse” the “Elements” of a “List” “Out-Of-Place” Using the “reversed ()” Built-In Python Method

“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”.
“Sort” the “Elements” of a “List” in “Ascending Order” “Out-Of-Place” Using the “sorted ()” Built-In Python Method

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”.

“Sort” the “Elements” of a “List” in “Descending Order” “Out-Of-Place” Using the “sorted ()” Built-In Python Method

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”.

“Sort” the “Elements” of a “String List” by the “Length of Its Elements” Using the “sorted ()” Built-In Python Method

--

--

Oindrila Chakraborty

I have 11+ experience in IT industry. I love to learn about the data and work with data. I am happy to share my knowledge with all. Hope this will be of help.