Everything you need to know about Python Lists- Part 2/2 (2022)

List Methods, Aliases/Memory location, List Comprehension

Parichay Pothepalli
3 min readApr 4, 2022

Lists have in-built methods that are available for processing. These methods make life easier as they have been already implemented on the basis of the most use cases available for lists in real-time programming.

Check out the first part of my Blog series to better understand and follow along.

  1. Simple addition:- Lists can be added to each other. They however do not produce a sum as it can be wrongly intuitive. They are used as concatenation operators. ( See implementation in Snippet 1)
  2. Simple Multiplication:- As you would have guessed, this operation as well does not produce a dot product of elements inside the list. The multiplication operator is used to replicate a sequence repeatedly as specified by the user. ( See implementation in Snippet 1)
Snippet 1: Addtion and Multiplication of lists

3. Append():- Append helps in passing an object as a parameter and then adding it to the end of the list.

4. Extend():- Adds the content of the object to the end of the list.

Note:- Though the Append() and the extend() method may sound similar, there is a significant difference and we shall explore it in the Snippet 2.

Snippet 2: Append() and Extend()

5. count():- Counts the number of times an element occurs in a list. ( See Snippet 3 for implmentation)

6. Index():- Returns the index of the first occurence of the argument( See Snippet 3 for implmentation)

Snippet 3: count() and index()

7. insert() -As the name suggests, the insert() method helps us in inserting a particular object at a specified index. It takes two parameters the, the object itself and the index at which it has to be inserted. ( See Snippet 4 for implmentation)

8. del()/remove()/pop():- All these methods are useful in their very own way and make changes to the intial list by removing elements.( See Snippet 4 for implementation)

Significant differences between all the three:-

del():- Removes a particular item from a list. It can be used to remove multiple items form the list using indexing. It does not return anything.

remove():- It takes an object/item as a parameter and removes the first matching value. It does not return anything.

pop():- It can take one parameter/ no parameters . It takes an index as an optional parameter. Otherwise it shall pop out the last item. It returns the element

Snippet 4: insert(), del(), remove(), pop()

9. sort():- Sort helps us sort the list in-place which means that it will not return any value( None object type). If the list contains more than one datatype of objects, it will throw an error on sorting.

Snippet 5: sort()

Aliases:

Python has everything figured out as an object. Each object when stored in the memory is given a specific id().

Note:- Two memory locations can have the same value, but the change of value in one memory location does not change the other.

However, if two objects reference the same object location, and one is changed then automatically the other object also gets changed.

To check for memory locations we can use the is operator, which returns True id the memory location is the same and False if otherwise.

To check for values being the same we use the ‘==’ operator.

Check the implementation in Snippet 6.

Snippet 6: Memory Location and Aliases

List Comprehension:-

List comprehensions provide a concise way to create lists. It consists of square brackets containing an expression followed by a for clause, then zero or more `for` or `if` clauses.

Snippet 7: List Comprehensions

As we have completed the entire List series. It is now time to explore the next Data Structure i.e Tuples.

--

--

Parichay Pothepalli

Data Scientist who believes that AI/ Deep Learning is here to change the way we view the world. Join me in this journey of growth, sharing experiences.