All The Basics to Begin With Python Lists!
This article was published as a part of the Data Science Blogathon
In this article, you’ll learn about lists in Python and their applications while we writing the python programs in an optimized manner. You will learn starting from what they’re, their syntax, and also see the examples to use them in the programs.
Note: If you would like to have an audio-visual experience while learning about lists in Python, You may go through the video lecture below. You may also refer to the video side-by-side with this article, to get a better grasp.
A list is a standard data type of Python that can store a sequence of values belonging to any type. They are depicted through square brackets. Eg:
[1,2,3] ; ['abc', 'a','b','c']
Creating and Accessing List:
The syntax to declare an empty list is :
Creating a list :
list_name = list(); # Or one can also write list_name[ ]
Creating List from existing sequences:
L = list(“abc”)
will result in the list L to be ‘a’, ‘b’, ‘c’
Read input from the user :
l = list(input('Enter the numbers')) enter the numbers: 12345 >>> l ['1','2','3','4','5']
Accessing a list:
The len() function returns the number of elements present in the list
a=[1,2,3,4,5] print(len(a)) 5
Difference between lists and strings:
The main and most important difference between strings and lists in Python is that Lists are mutable, while strings are immutable, meaning, we can change the individual elements at a given place in a list, but not in the case of a string.
Traversing a list:
We can easily traverse a list in python using a for loop. Below is a demonstration:
L = ['P','y','t','h','o',n'] for i in L: print(i)
Output will be : P y t h o n
Joining lists is pretty easy. We can use the concatenation operator ‘+’ to join two lists.
>>> l1 =[1,2,3,4,5] >>> l2 = [6,7,8,9,10] >>> l1+l2 [1,2,3,4,5,6,7,8,9,10]
Repeating or replicating lists :
Like strings, we can use the multiplication operator ‘*’ to replicate a list specified number of times.
>>>l =[1,2,3] >>> l*2 [1,2,3,1,2,3]
Slicing the lists :
We can use the indexes to extract the list slices. (Remember the ending element will be the one preceding the element, whose index is given as the second parameter).
L= [1,2,3,4,5,6,7,8,9,10] print(L[2:7]) Output will be : 3,4,5,6,7
Here, although the specified ending parameter was 7, traversing stopped at 6
Quick trick: To reverse a given list, use the code :
#assuming the list variable is L L[: : -1] # this would return the reversed list
Working with List :
Appending elements to list: To append an element at the end of a list, we use append() function.
L=[1,2,3] L.append(4) print(L)
Updating elements in a list : Syntax is L[index] =
L = [1,2,3,4,5,7,7] L =6 print(L) 1,2,3,4,5,6,7
Deleting elements from a list: The del method deletes the first occurrence of the element from the list.
Syntax : del list_name
If we write del list_name, the entire list would be deleted.
We can also use the pop() function, to delete an element from a given index. If no index is specified, it removes the last element.
Syntax : list_name.pop()
List Functions and Methods :
Index method :
The function returns the index of the first matched element from the list. If the element is not present, it raises an error.
Syntax : List_name.index()
The append method :
As we have read earlier, this function adds the specified element at the end of the list.
Syntax : List_name.append()
The extend method :
The extend method is used to add multiple elements in the list at the same time.
Syntax : List_name.extend()
Difference between append() and extend() :
The difference can be better depicted with an example.
>>>t1 = [1,2,3] >>> t2 = [4,5]
Here, t1.append(t2), would return [1,2,3,[4,5]], but on the other hand, t1.extend(t2), would return [1,2,3,4,5].
This means, append() adds the value, be it a single value, or even a list, to the end of the list, as a single entity, unlike, extend()
The insert method :
The insert method is used to insert an element in a given list at a specified index. When we have to insert an element in a given index, we cannot use append() or extend(), we have to use insert() then.
Syntax : List_name.insert(,)
The clear method:
This method clears all the elements from a list. If a list Ls =[1,2,3,4,5]
It gives [ ] as output.
The count method :
Returns the number of occurrences of the element in the given list.
Syntax : List_name.count()
The reverse method:
As the name suggests, it reverses a list.
Syntax : List_name.reverse()
The sort method:
This method sorts the array in ascending order. (Can even arrange letters lexicographically)
(Do note: using this method, and also the similar typed methods, in interviews is not at all recommended as it comes pre-built, and all you had to do is implement it. But you can definitely use it while practicing lengthy programs, to shorten the process.)
Syntax : List_name.sort()
Ending Notes :
Thanks a lot for reading..!!
If there is anything that you would like to add to this article, please let me know. Also, I’d love to get some suggestions about what I should write about in the upcoming articles. Any kind of constructive criticism is always appreciated and will be greatly acknowledged. I always love to chat with like-minded people.
If you liked reading this article, do share this article with your friends, to show some love.
About Me :
Heyy, I am Pinak Datta, currently, a second-year student, pursuing Computer Science Engineering from Kalinga Institute of Industrial Technology. I love Web development, Competitive Coding, and a bit of Machine Learning too. Please feel free to connect with me through my socials.
Till then, Ciao Adios, and have a great day ahead….!!!!!