Python Lists(Array) and Its Functions
Imagine that you are in a class and you want to store all the 40 student’s marks in a variable. But it’s not a good idea to create 40 more variables and if you just keep defining variables with different values, its value will be overwritten. Yes, there is of course a way to do this and which is using Lists
Lists
Lists are one of the most used variable types in python. A-List is a Mutable data type and can hold multiple values. We aren’t limited to store only numbers but we can also store ‘strings’ and ‘objects’ and ‘dictionaries’ in it.
How to Create A-List
A list in python can de be declared simply by putting square brackets (‘[]’) and adding the sequence of items in it(which is of course following the variable name ). Here is how to create a list:
Now an obvious doubt would be “How to use these values now?”. Ok let’s understand it below
Access List Items
Like we did string slicing from Part 1 of this course, we can also access items in a list using square brackets (‘[]’) and it follows the same rule that the first item has an index of zero, the second item an index of one and so on. I got an interesting image that explains it:
Slicing in List
Slicing in the list is mostly all the same as we did in string slicing. As said before you must check Part 1 of this course so that you can get an idea of what are we doing here. But here is how we do it in a list and you can also recap what you learned earlier.
Using list slicing(or string slicing), we can take some portions of the list from the original list. And an important thing to note is that they do no change the original list instead returns a new list.
Let’s assume we have a List called ‘My_List’. We can take a portion of it by adding square brackets (‘[]’) and by providing the ‘start index’ and the ‘stop index’ separated by a colon. We can also tell the function to skip how many items.
Syntax
My_List[start:stop:step]
Usage:
If you want you can keep the values blank too. Blank values get replaced by the default values. For example, if you keep the start as blank it gets replaced by the zero(first index), and similarly in the ‘stop’ and ‘step’ case
My_List = [1,"one",2.0,'two',True,False]print(My_List[:6]) #(start index default)print(My_List[0:]) #(end index default)print(My_List[:]) # prints ['two', True] # (both values default)print(My_List[0:3:]) # prints [1, 2.0, True] # (skip step default)print(My_List[::]) # prints [1, 2.0, True] # (all default)
Now let’s discuss some of of the list methods
List Methods
- Append
Adds an item to the end of the list
My_List = [1,"one",2.0,'two',True,False]
My_List.append("three")
print(My_List) # prints [1, 'one', 2.0, 'two', True, False, 'three']
- Insert
Adds an element at the specific position(index) in a list. This function requires an index to know where to insert and the element to insert. Let’s insert an element at the index 2 of the list:
My_List = [1,"one",2.0,'two',True,False]
My_List.insert(2, "four")
print(My_List) # prints [1, 'one', 'four', 2.0, 'two', True, False]
- Pop
The last element is removed from the list.
My_List = [1,"one",2.0,'two',True,False]
My_List.pop()
print(My_List) # prints [1, 'one', 2.0, 'two', True](No 'False')
- Remove
Removes the specified value from the List.
My_List = [1,"one",2.0,'two',True,False]
My_List.remove("one")
print(My_List) # prints [1, 2.0, 'two', True, False] (no 'one')
- Clear
Removes all the elements from the list (empties the list )
My_List = [1,"one",2.0,'two',True,False]
My_List.clear()
print(My_List) # prints []
- Reverse
Reverses the order of the list
My_List = [1,"one",2.0,'two',True,False]
My_List.reverse()
print(My_List) # prints [False, True, 'two', 2.0, 'one', 1]
- Sort
Sorts the list either in ascending or descending order. - You can check out the full use of this from Here
Loops In List
As we did ‘for loops’ and ‘while loops’ from the Loops and Ranges course to loop through strings, now, let’s loop through Lists.
1) Using ‘For’ Loop
We can easily loop through lists using the ‘For’ Loop. Let’s Do it in code:
It would print all the items given in the list(in our case numbers from 1 to 10)
2) Using ‘While’ Loop
Let’s Iterate using while loop in a List:
3) Using Range Function
Let’s integrate using the range function and it is one of the most common ways to do it
Ok, this all was about lists in python. If you want to know about dictionaries you can check it from here. Till then stay safe stay healthy!!
This post was originally published at https://www.decodebuzzing.com
Thank You !!!