[008]Introduction to Python Basics: List Methods 2

Koay Yong Cett
4 min readJun 27, 2020

--

Photo by Joshua Eckstein on Unsplash

Let’s explore other list methods in Python.

Index:

  • Here, we can see that we can give index a value, a start and a stop. What does that mean?
  • We get the index of 3 as the letter d position is located at index 3 in our memory.
  • We can also do an optional parameter here. Here, we will look for the d from index 0 to index 2(before it).
  • Why do we still get error? This is because the search stop right before the index of 3.
  • We are only able to get the d position when we set the stop to 4.

Python Keywords:

source: https://www.w3schools.com/python/python_ref_keywords.asp
  • We may also use the Python keywords that is very useful in some manner.
  • Here, we can see whether the letter is in the list basket or not. True for the one that exist and False for the one that doesn’t exist.
  • The “in” keywords is also able to use in this way.

Count:

  • We can count how many times an item occurred. Here, we can see that the result is one as the “d” only exist once in the list.
  • If there is two letter “d”, then we get two as it count how many times is letter “d” in the basket list.

Sort:

Let’s get what sort is going to do…

  • Sort allow us to input two number which is key and reverse.
  • We get none when we run like this since the sort return nothing.
  • Here, we sort the list and print the sorted list out. We get a sorted list where d is move in front of e.
  • If we add x at the beginning, then the x will be moved to the end of the list. The sort is used to sort the list when x is moved to the end and same goes for the other.

What about the Built In Functions Sorted()?

  • The list is not sorted in this way of coding and why?
  • I know that it’s confusing for having two kind of sort. We are able to get the sorted list when we print the sorted basket. The sorted produce a new array/list or what we called a copy of the list.
  • As we can see that the basket hasn’t been modified and it still remain the same. On the other hand, sorted doesn’t modify the basket in place but it creates a new copy of basket and sorts it.
  • Remember the list slicing where we make a copy of the original list. Here, we see that we get the same result with the print sorted basket.

Copy:

  • Instead of the list slicing, we can use the copy method and get the same result. This copy method copy the original list and yields a new copy/new list

Reverse:

  • The reverse method reverses the basket list in place. It is not similar to the sort method/sorting and it’s simply switching all the indexes into opposite sides.
  • We sort first and then reverse the list. Then, we will have the sorted reversed basket list.

Thanks for reading my stories 😃

--

--

Koay Yong Cett
Koay Yong Cett

Written by Koay Yong Cett

Every stories I shared is based on my personal opinion. Interest in ethical hacking and penetration testing. Thank you.

No responses yet