A quick yet complete tour of lists in Python3 in just seven minutesPALAKOLLU SRI MANIKANTABlockedUnblockFollowFollowingApr 5Photo by Emma Matthews on UnsplashPython lists are not like arrays.
They are bit different.
When it comes to dealing with array’s we talk about a collection of homogeneous data elements.
This is not true in case of a list in python.
Python List can store a heterogeneous collection of elements.
This feature will help developers and programmers to work with lists in a more flexible manner.
A List in python is one of the most powerful inbuilt data structures.
Lists in python can store Integers, Floating values, Strings, Boolean values, and complex values as well.
How to create a List in pythonWe can create a list in python in two waysBy declaring a variable with an empty square brace i.
e By using list().
Example# Here first I'm creating a my todo list which is used to store my to-do activities.
myTODOList = # The above line will create a list object for me# I'm creating a another list which will store my general information.
myGeneralInfo = list()# The above line will also create a list object for me# Getting the types of list objectsprint(type(myTODOList))print(type(myGeneralInfo))OutputOutput for the above few lines of code.
It’s amazing at this point you are able to create a new list object with most frequently used methods.
Now we will move forward on how we can add new elements to our list and many more things.
Let’s get started.
How to add Data to our list?First of all, I would like to introduce the concept of Mutability.
Mutability means the ability to change its behavior.
Python lists are mutable in nature.
We can add or delete elements from the list.
This is one of the biggest advantages that attract programmers to work with lists when compared to other inbuilt data structures.
We can add elements to a list in two ways:By using append()By using insert()By using append()With the help of the append method, we are able to add one element at a time.
This method will help us to add elements at the end of the list only.
The syntax for the append function is —listName.
append(item/element)# Adding Elements to the listsmyTODOList.
append('Wake up Early Morning')myTODOList.
append('Go to Gym')myTODOList.
append('Play Some Games')myTODOList.
append('Get ready to go the college')myTODOList.
append('Go to library')# Printing the entire list elementsprint(myTODOList)OutputOutput for the above line of code.
By using insert()This insert method is used to add the elements at a specified position in the given list.
The syntax for the insert function is —listName.
insert(position, item/element)insert() uses two parameters — position and list item.
The position is where the element is needed to be kept in the list.
These positions are generally called as indexes.
Usually, the list index in python starts from 0.
e the first element index is 0 and the second element is 1 and the third element index is 2 and so on).
From this, we can conclude that —A list of n elements will have at most an index number of n-1 i.
e A list with 5 elements will have maximum index value of 4.
Example# Adding Elements to our list with the help of insert()myGeneralInfo.
insert(0, 'Paid the Library Fee')myGeneralInfo.
141521)# Printing the myGeneralInfo list informationprint(myGeneralInfo)OutputOutput for the above few lines of code.
myGeneralInfo is filled with some random information for illustration purposes only.
How to Access the List ElementsWe can access the list of elements by using the following two ways:By using an index operator.
By using slice operatorBy using an index operatorWe can directly access our list elements with the help of the index operator.
Example# Acessing the certain values from the listprint(myTODOList)print(myTODOList)print(myTODOList)OutputOutput for the above programBy using Slice OperatorThe slice operator is one of the most commonly used operators to access the list elements effectively.
The syntax for the slice operator is:listName[start: stop: step]start — It indicates the index where the slice has to start.
The default value is 0.
stop — It indicates the index where the slice has to end.
The default value is the maximum allowed index of list i.
length of the list.
step — Increment value.
The default value is 1.
Example# Getting the information using slice operatorprint(myTODOList[0:3]) # we don't need to specify the step value.
print(myTODOList[2:4:1])print(myTODOList[0:4:2])OutputOutput for the above few lines of code.
Python lists are iterable objects.
For any iterable object in python, we can write a for loop to print out all the data.
Example# Iterating over the listfor item in myGeneralInfo: print(item)Output for the above lines of code.
How to remove an element from the listWe can remove the list elements with the following two ways:By using remove()By using pop()By using remove()remove() is used to remove the element that is specified to it.
The syntax for remove() is:listName.
remove(item/element)# Deleting the element from the listmyGeneralInfo.
remove('Paid the Library Fee')# printing the result after deleting the elementsprint(myGeneralInfo)After deleting the list elements the output would be as followsBy using pop()It is an iterator method which is used to remove the single (or) multiple elements at a time.
It deletes the elements from the back side.
The syntax for the pop() method is:listName.
pop()# printing the list items before deletingprint('My TODO List Elements: ',myTODOList)print('My General list Elements: ',myGeneralInfo)# Deleting the list elements using pop()myTODOList.
pop()# Deleting the list elements completelyfor item in range(len(myGeneralInfo)): myGeneralInfo.
pop()# printing the resultsprint('My TODO List Elements: ',myTODOList)print('My General list Elements: ',myGeneralInfo)This is the way we can delete list elements using pop()In the above program, we used the len() within the for loop.
len() is used to give the length of the list i.
number of elements present in the list.
Various Attributes and Functions on List Objectpython dir() function is used to give the set of inbuilt attributes and methods that are associated with it.
Example# Printing all the attributes and functions on the list objectprint(dir(myTODOList))OutputVarious Attributes and Methods on the list objectVarious List Methods and their use:append() — It will add an element to the end of the list.
clear() — It is used to remove all items from the list.
copy() — It is used to return another copy of the list.
count() — It is used to return the count of the number of items passed as an argument.
extend() — It will add all elements of a list to another list.
index() — It is used to return the index of the first matched item.
insert() — It is used to insert an item at the defined index.
pop() — It is used to remove and return an element at the given index.
remove() — It is used to remove an item from the list.
reverse() — It is used to reverse the order of items in the list.
sort() — It is used to sort items in a list in ascending order.
When to use List Data Structure?If you want to store multiple data objects, insertion order must be preserved.
If you want to store duplicate values as well, then this data structure will be more helpful to perform such operations.
I covered pretty much everything that is required to perform any kind of operation on the list data structure.
Hope this helped you learn about Lists in python in a quick and easy way.
If you liked this article, please click on the clap and leave me feedback.
Please share with your friends.
.. More details