Hi coders, In this article, I’m gonna explain you Lists in Python. Basically we’re going to explore basic of lists in python and also we will explore methods and operation that you can perform on lists.
Python Lists
In python, lists are used to store data of different data types in ordered form. You can access objects of list using index number as same as strings. But Lists are python built-in data structure, where you can store your different types of data and then further you can modify them as per your need.
In Python, you can create a list using brackets “[]”, e.g: [2,4,6,8,10].
Now, I’m gonna show you what you can do with python lists, basically I’m going to explain all the important methods and operation that you can perform on these lists.
As I said, You can have different types of data inside a list, so, you can also define a list of lists e.g
[[1, ‘prashant’], [2, ‘ritesh’], [3, ‘sohail’]]
In the above, you can see we have 3 lists inside a parent list. and each child list contains two different types of data [integer, ‘string’].
Basic Lists Operation:
Suppose, you want to create a list with 100’s of 1, instead of writing 100 times 1 e.g: [1,1,1,1,—————–,1]. You can create such type of list using * operator:
long_list = [1] * 100
print(long_list)
# Now this create You a list with 100's of 1
Lists concatenation:
You can also concatenate list in python using + operator, e.g:
first_list = ['prashant', 'sohail']
second_list = ['mahure', 'ritesh']
# Concatenation
combined_list = first_list + second_list
print(combined_list)
# Output: ['prashant','sohail','mahure','ritesh']
list() Function uses:
You can also use list function to create lists, for example: If you want to create a list with a range between 0-19 then you can use list function for that:
print(list(range(20)))
# Output : [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
print(list('Prashant')) # It will create a list with each character
# Output : ['P','r','a','s','h','a','n','t']
Accessing items from List:
In terms of accessing items out of the list is more similar to strings and arrays. You can access any object of list by using their index no.
name = ['prashant', 'sohail', 'ritesh', 'mahure']
# name = index no. like [0,1,2,3]
# Let's assume you want to access name "sohail", then you can access it by using their index # no. 1
print(name[1])
you can also access objects of list between specific range of index using ‘:’ :
name = ['prashant', 'sohail', 'ritesh', 'mahure']
print(name[:2]) # To access all objects before index no.2
#['prashant','sohail']
print(name[2:]) # To access objects from index no. 2 to last index
#['ritesh','mahure']
print(name[1:3]) # To access objects from index no. 1 to index no. 2
Unpacking List in python:
This is one of the best things that you can do with python lists. You can unpack-objects of a list and assign it to variables.
# Let's unpack a list of roll_no and assign it to different name, Here, roll_no would be list # and name will be our variable.
roll_no = [101, 102, 103, 104]
pk, sohail, ritesh, mahure = roll_no
# Now here pk is a vaiable and it roll no will be first element of list. (pk=101)
But suppose, you have a list from [1,2,3 —-, 10]. and right now you just have few names to assign. Then you can assign a few objects to those names. And the rest of them you can pack in a separate list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
first, second, third, *others = numbers
# Now here first=1, second=2, third=3, rest of the objects are packed inside separate list # called others. To pack the rest of the element, you need to attach * argument before
# variable
print(others)
# Output: Others [4,5,6,7,8,9,10]
Adding and Removing items to List:
Now let’s see, how you can add and remove items to a list. There are various ways to add and remove items to list.
For example: we have a list numbers = [1,2,3,4]. and now we’re going to add and remove items to this list using different methods:
Removing items from list using Pop() method:
By using pop() method you can remove items from list directly. Pop() method takes one argument(index no.). You have to enter the index no. of an element that you want to remove from the list. If you wouldn’t enter any index_no. then it will automatically remove the last object from list.
numbers = [1,2,3,4]
numbers.pop()
print(numbers) # It will remove 4 from the list
numbers.pop(1) # It will remove the item from index no 1, which is 2
print(numbers)
Removing items from list using Remove() method:
You can also use remove() method to remove item from list. But it takes value as an argument instead of index no.
Suppose, we have a list numbers = [1,2,3,4,5]. So, If you want to remove 3 from the list, then you need to enter value 3 inside the remove() method as an argument.
numbers = [1,2,3,4,5]
remove(3)
print(numbers)
# Output: [1,2,4,5]
Removing items between specific range using del keyword:
By using del keyword, you can remove items between specific range. Format of using del keyword: del list_name[starting_index_no : end_index_no+1 ]
num = [1,2,3,4,5]
del num[1:3]
print(num)
# Output: [1,4,5]
Adding items to list using append() method:
You can add items to list using append method, You just need to put a value inside a append(value) method and it will add that item to list,
numbers = [1,2,3,4]
numbers.append(5)
print(numbers)
Output: [1,2,3,4,5]
But if you want to add item to list at specific position, then you can use insert() method for that.
Adding item to list using insert() method:
You can also add item to the list using insert() method at specific position. insert() method takes two arguments, first is index_no. and then second is value.
numbers = [1,2,3,4]
numbers.insert(1, 23)
# it will add 23 no. at index_no. 1
print(numbers)
# Output: [1,23,2,3,4]
These were the basics of python lists. But there are lot more things about lists that you can read from python docs list
Read also: Exception Handling in Python(Guide)
I hope you liked this article. If yes then please don’t forget to share this article with your friends. You can also subscribe our blog via email to get future updates
Thanks to read article.
1 thought on “Python Basic: Lists methods and operations (Basic Guide)”