List In Python — Data Structure
This blog is originally published on my website: LionGuest Studios.com
Understanding the Data structures is the most crucial and important thing when it comes to learning and working with any programming language. Python too has some of the most popular data structures using which you can organize the data in memory. List, Tuples, Sets, and Dictionary are some of the widely used data structures in python. We will discuss the List data structure in python in detail.
List In Python
- The List is a data structure in python which stores data linearly or sequentially one after the other. It is somewhat similar to arrays in other programming languages.
- The only difference between an array and a List is the arrays can store data of the same type whereas Python’s List can store data with multiple data types (a combination of integer, string, floating-point, etc).
- Lists are represented using square brackets [ ]. The initial item in a list starts with an index number ‘0’.
- The list is a mutable data structure which simply means that once it has created the items inside which can be changed later.
Creating a list in Python
Lists can be created by putting values inside the square brackets and assigning it to a variable.
empty_list = [ ]
Here, empty_list is the name of the variable which will contain a list as represented by square brackets. And inside the square brackets, you don’t have any elements which simply means that the list is empty.
Examples:
list_of_numbers = [ 1, 2, 3, 4, 5]
list_of_Strings = ['Ana', 'Pat', 'Simon', 'Amanda']
Note that the strings inside the list are represented by quotes (single or double quotes anything will work) and numbers have no quotation mark.
list_with_different_data_types = [ 'Micheal', 15, 20.5, [1, 2, 3] ]
This is a list that contains multiple data types. Also, the lists can contain another list too. In this case, element number 3 is an element of a list which is a list in itself.
Adding elements to a list in Python
There are two ways in which you can add an element to a list. The extend() function will be discussed later in this blog post.
1. append() function:
If you want to insert an element at the end of the list then you can use the append function. Inside this function, you have to pass the value or the element that needs to be inserted in a list.
original_lst = ['A', 'B', 'C', 'D', 'E']
original_lst.append('F') #This will insert element 'F' at the end of the list print(original_lst)Output:
['A', 'B', 'C', 'D', 'E', 'F']
You can even insert an entire list to an already created list with the append() function.
lst = ['A', 'B', 'C', 'D', 'E']
new_lst = ['F', 'G', '1', '2', '3']
lst.append(new_lst) print(lst)Output:
['A', 'B', 'C', 'D', 'E', ['F', 'G', '1', '2', '3']]
Note: If you want to add a single element to a list then use append() and if you want to add multiple elements into a list then use extend(). Go to extend() function.
2. insert() function:
If you want to insert an element at a specified location in a list then you can use the insert(location, element_to_insert) function which takes two parameters. In the first parameter, you have to pass the location at which the element is to be inserted and in the second parameter, you have to pass the value of the element.
my_list = ['one', 'three']
Now let’s add element ‘two’ to the above list at index 1 for that you have to use the insert function.
my_list( 1, 'two' )
After inserting the element ‘two’ at index 1, the element which is currently present at that location will be moved to the next index (in this case at index 2).
Output:
['one', 'two', 'three']
Deleting elements from a list in Python
- remove() function:
If you want to delete a particular element from a list using its value then you can use the remove() function. For example, if you want to remove element ‘ B ‘ from the list then you have to pass this as an argument to the remove() function.
list = ['A', 'B', 'C', 'D', 'E']
list.remove('B') print(list)Output: ['A', 'C', 'D', 'E']
2. del keyword:
If you want to delete a particular element from a list using its index then you can use the del keyword. Let say you want to delete ‘Marry’ from the below list then you can delete that element using its index no.
list = ['Merry', 'Adam', 'Watson', 'Anna']
del list[0] #passing index 0 because Marry is present at the index print(list)Output: ['Adam', 'Watson', 'Anna']
3. pop() function:
The pop() function works the same way as the del keyword but the only difference is that the pop() function will also return the element which has been deleted from the list.
list = ['Merry', 'Adam', 'Watson', 'Anna']item_popped = list.pop(0) print(item_popped) print(list)Output: Marry ['Adam', 'Watson', 'Anna']
So, If you want to delete an element from a list in python using the value then use the remove() function. And if you want to delete an element using index no then use the del keyword or pop() function. In case if you also want to get the item that you have deleted then use the pop() function which will return you that item.
Accessing elements inside aList (Indexing) in Python
Each element in a list as an index no starting with zero. To find an element inside the list you have to use its index no. Accessing an element using its index no is called indexing. This can be done by passing the index number inside the square brackets.
list[indexNo]
In the below list, if you want to access the individual elements inside a list then you can pass the index as follows.
my_list = ['one', 'two','three']
print(my_list[0]) # accessing element 'one'
print(my_list[1]) # accessing element 'two'
print(my_list[2]) # accessing element 'three'Output:
one
two
three
Slicing a list in Python
Slicing is nothing but dividing a list. In case if you want to print some part of the list and not the entire list then you can use slicing.
Slicing is done using “:” colon symbol. It has 3 parts list[ start : end : stepSize ]. Step size is optional.
Let’s take an example and understand this.
slice_this_list = [10, 20, 30, 40, 50, 60]
So, to slice them you have to use the index number of both the start and the end elements. The index of starting element 20 is 1 and the index of ending element 50 is 4. You will use these index nos in order to slice the list.
print(slice_this_list[1 : 4])
What do you think it will print? It will print 20, 30, 40 only 3 elements. But we also want to print 50, right? The last element is not included in the slicing because python will exclude the end and will stop at one index before the end i.e in this case index 3.
To read the full blog post visit: LionGuestStudios.com
That’s all it is. Thanks for the read. If you like the content then support us on Patreon. Your support will surely help us in writing more of such content.
To read more such blogs about Python programming related stuff visit our blogs page on LionGuest Studios.
Originally published at https://liongueststudios.com.