Lists in Python
This is the fifth article of the Python crash course. In the previous article we have discussed the concept of strings in python, we've also covered string formatting in python. In this article we will discuss the concept of lists in python. We are already familiar with the sequences(in strings).
In this section we will learn about:
1.) How to create lists
2.) How to use indexing and slicing in lists
3.) Inbuilt list methods
4.) Nesting in python lists
5.) Intro to List Comprehensions
How to create lists¶
# Lets create a variable with the name of list_one
list_one = [1, 2, 3, 4]
In the above cell we have created a list of integers, but it is upto you that what type of objects you fill in the list you created.
Lets take an example:¶
list_one = ['Ankush Pandey', 'a', 23, 3.5, 'bob']
We have already discussed the use of len method, we can use len method in this case also to get the total number of elements we have in the list, for example let's print the length of the above defined list.
print(len(list_one))
List indexing and slicing¶
As we have discussed in the previous article, the concept of slicing is very much similar to string.
list_one = ['Ankush Pandey', 'a', 23, 3.5, 'bob']
# print the elements at various indices.
list_one[3]
# print elements starting from element at index two and going till the end
list_one[2:]
# print elements starting from element at index zero and going till the fourth element
list_one[:4]
Just like the strings we can concatenate two lists using the '+' operator
list_one + ['new name', 568, 13.5]
The above cell didn't change the actual list
list_one
Now let's update the list
# reassignment to update the original list
list_one = list_one + ['new name', 568, 13.5]
print(list_one)
We can also use the * for a multiplication method similar to strings:
# Now lets multiplicate the original list
print(list_one * 2)
# The original list is not altered
print(list_one)
Inbuilt list methods¶
Lists might sound like the arrays in other programming languages, a sequence of sumbers, but they are cirtainly not. Because unlike the arrays lists dont have restrictions to store the same data type. Let's look at some of the methods of list.
# let's create another new list
list_1 = ['a', 4, 7, 5, 'd']
append() method: to add items in the last of the list.
list_1.append('first item')
list_1
pop() method: pop method is just opposit of the append method, it is used to pop items from the list. By default it removes items from the last of the list and return it, but when passed with the index it can remove any specified element and return it.
# Let's pop the element at the index 2
list_1.pop(2)
list_1
# Let's save the poped item in a variable and display it
popped_item = list_1.pop()
popped_item
list_1
If we try to access unknown index (index more than the capacity of the list) it will show an error.
list_1[555]
sort() method is used to sort the list.
reverse() method is used to reverse the list
It is clear that the method names in python programming are quite convincing and meaningful.
list_2 = ['e', 'a', 'u', 'o', 'i']
list_2
# now let's sort the list
list_2.sort()
list_2
# now let's use the reverse() method to reverse the list
list_2.reverse()
list_2
Nesting in Python lists¶
Now let's create an anology of 2D arrays using python lists. I am using the word analogy because lists in python are very different than array in any other language.
So, let's use nesting to create 2D arrays
list_1=[9, 8, 6, 4]
list_2=[3, 2, 4, 1]
list_3=[9, 6, 8, 7]
# Now we will cfreate a list of lists so lets see that
list_2d = [list_1, list_2, list_3]
print(list_2d)
Till now we have used single level of indexing to access any element in a 1D list, but now in the case of nested lists we will have to use multilevel indexing to access any element.
# Let's see what is the first element in the list
list_2d[0]
# Now we will access the first element of the first element of the list
list_2d[0][0]
Intro to list Comprehensions¶
Now let's take a very quick look at the concept of list comprehension. This is a feature of Python language where you can create lists along with performing various operations simultaneously, very easily.
We will cover this topic in a separate article, till then. Let's take an example of list comprehension.
# Lets create a list using for loop
comp_list = [row[0] for row in list_2d]
comp_list
If you are not able to understand the comprehension topic, don't panic we will cover comprehension in a separate article.
Thankyou

nice✌🏾
ReplyDelete👌👌
ReplyDelete