Lists in Python!!!


Python is a very interesting language. It is used everywhere today.
This article explores how the lists in python and how you can get more out of them. It will make you confident in lists
What is a LIST?
In python list is a data structure that is used to store some information. So if you are creating some application where you need to store the details about a particular user then you can use lists.
This was just one interesting example we can do a lot more using python lists.
Basic code to make a list is as follows
mylist = ['apple','mango','this','that']
The above list stores strings as its items we can also form a list which stores numbers as its elements and we can form a list that stores both of them. We can also form a list which can store other types of data than strings and numbers like boolean values.
Let us explore the this lists ahead.
The sorting method
In python to sort a list we use .sort() if you don’t use () then you will get some weird looking memory address, we don’t want that.
Our purpose is to sort a list so we don’t need a memory address
Consider the list below
mylist = ['apple','mango','pear','grapes']
On applying the .sort() method on this list we get an alphabetically sorted list. Python sorts a string list in alphabetical order.
mylist = ['apple', 'grapes', 'mango', 'pear']
Here is another interesting question what happens if I have a word with capital letter?
mylist = ['apple', 'grapes', 'mango', 'Pear']
Python gives more priority to a capital letter word so on sorting Pear will go ahead
mylist = ['Pear', 'apple', 'grapes', 'mango']
If we want to sort a list in the reverse order we can give the sort function reverse = True argument and then it sorts the lists in the reverse order
REMOVE from the lists
There are many methods to remove elements from the list.
Some of the common methods are described below.
.remove: It replaces the element from the list but only removes the element's first occurrence.
mylist = ['Pear', 'apple', 'grapes', 'mango','Pear'] mylist.remove('Pear') mylist = ['apple', 'grapes', 'mango','Pear'] #output
.pop: This method is exciting. It removes the element at a particular index. Just give the index of the list element and it will remove the element. If you do not give the element index then it will replace the last element of the list. It returns the removed element as an output.
mylist = ['Pear', 'apple', 'grapes', 'mango','Pear'] mylist.pop(0) mylist = ['apple', 'grapes', 'mango','Pear'] #output mylist.pop() mylist = ['apple', 'grapes', 'mango'] #output
.clear: It clears all the elements of the list, list becomes empty but it still remains.
mylist = ['apple', 'grapes', 'mango'] mylist.clear() mylist = [] #list is empty now
.del: This keyword deletes the whole list. After using this keyword the existence of the list comes to an end.
mylist = ['apple', 'grapes', 'mango'] mylist.clear() mylist #on calling, it gives an error
List COMPREHENSION
You can move the mountains with this feature.
There are 3 main parts to focus on while writing list comprehension, [modifier-loop for iterable - filter].
All of these can be modified to get the desired output through conditionals and other tools in python.
Below is the basic example of list comprehension:
lst = ["apple","mango","pear","grapes","banana"]
mylist = [x for x in list]
Here notice that we haven’t used any filter here, we are using for loop to iterate over the list called lst.
Our output part contains only “x” which means that we get only elements as outputs without any modifications.
Now, let us try to modify the elements that come out while iterating through the loop
lst = ["apple","mango","pear","grapes","banana"]
mylist = [x.upper() for x in list]
lst = ['APPLE', 'MANGO', 'PEAR', 'GRAPES', 'BANANA'] #output
We can modify the output in many ways just like above!!
lst = ["apple","mango","pear","grapes","banana"]
mylist = [x.lower() for x in list]
lst = ["apple","mango","pear","grapes","banana"]#output
We can also set up a filter so that the elements with the desired properties only, reach the output.
fruits = ["apple","mango","pear","grapes","banana"]
newlist = [x.upper() for x in fruits if x!="apple"]
newlist = ["Mango","mango","pear","grapes","banana"]
This filter after the for loop changes is an if condition which basically states that if some element of the list is not “apple” only then return elements to the modifier which then converts all those words into capital letters.
You cannot use else condition here as it is just a filter so you can filter elements using if condition only.
But we can use if-else statements in modifier part so that we can get the desired output. Below is a basic example
fruits = ["apple","mango","pear","grapes","banana"]
newlist = [x if x!="apple" else "Orange" for x in fruits]
newlist = ["Orange","mango","pear","grapes","banana"]
As the outputs come out of the filter we can modify it using if condition.
Here, we are saying that if the element from the filter is not “apple” then return it as it is and if it is “apple” then change it to “Orange”.
We can also combine all of the things into one statement that looks a little bit Thanosian.
fruits = ["apple","mango","pear","grapes","banana"]
newlist = [x if x!="apple" else "Orange" for x in fruits if 'a' in x]
newlist = ["Orange","mango","pear","grapes","banana"] #output
In this case filter is letting only those elements out which have letter ‘a’ in them and then they are passed to the modifier which checks if the string is “apple” or not, if it is then it replaces “apple” with “Orange” and we get no “apple” in our list at all.
FINAL WORDS
THANKS for reading this article. I hope I have provided you with some value.
Follow to support and you can subscribe to my newsletter too, it has only 1 subscriber (and that’s me!!!)
Subscribe to my newsletter
Read articles from Kartavay directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kartavay
Kartavay
I write about things in order to make them INSANELY SIMPLE