Importance of Append
When choosing a collection type, it is useful to understand the properties of each type and choosing the most appropriate type for a particular data set. To know the most appropriate collection type you need to know the attributes of all the available types and then choose one from it based on your use case. In this article, we will go through the List Collection Type along with the append method in the article.
Lists are similar to the arrays, declared in other languages. Lists need not be of the same data types always which makes it a most powerful tool in Python and is the main difference between arrays and lists. A list can contain data Types such as Integers, Strings, as well as lists. Lists are mutable, which means they can be altered even after their creation.
List in Python are indexed and have a definite count while initializing. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index and the last item index is n-1 where n is the number of items in a list. Each element in the list has its indexed place in the list, which allows duplicating of elements in the list i.e we can create the same indexed item as another one with a different index and the list will still accept it, unlike Sets.
In Python, lists are created using the square brackets and each item inside a list is separated by commas.
my_list = [‘I’ ,”think” ,”Medium” ,’is’ , ‘number’ ,1] type(my_list) Output: list
You can see from the above code that while creating a list I have given both string and numeric datatype as items inside a list.
Indexing in Lists:
The list index starts with 0 and ends with n-1.
In our list index starts with 0 and ends with 5 which we can check using the pre-built len() function.
len(my_list) Output: 6
We can also check each item value based on its index as below.
my_list,my_list Output: (‘I’, ‘is’)
Till now we discussed on Collection type List now let’s move on to the append method in Python.
The append() method in python adds a single item to the existing list. It doesn’t return a new list of items but will modify the original list by adding the item to the end of the list.
After executing the method append on the list the size of the list increases by one.
The append() method takes a single item as an input parameter and adds that to the end of the list.
The items inside a list can be numbers, strings, another list, dictionary.
The append() method only modifies the original list. It doesn’t return any value as a return but will just modify the created list.
1. Adding Number to a List:
In the code below we will look at how to add a new numeric item to a list.
# list of strings string_list = [‘Medium’,’Python’,’Machine Learning’,’Data Science’] #adding a new int item to the string_list string_list.append(1) #printing appended list print(string_list) Output: [‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’]
2. Adding new list to a List:
Apart from adding a string and a numeric data type we can also add separate list to a list as below
#lets create a new list new_list = [1,2,3,4,5] #append this list to our string_list string_list.append(new_list) # print the appended list string_list Output: [‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’,[1,2,3,4,5]]
You can see from the above output that a new list is appended at the end of our old list. We will get the whole list as an indexed item using the below code.
string_list Output: [1,2,3,4,5]
If you want to access the elements from this list you can do that in the same way as you access elements from a 2-D Matrix.
string_list Output: 2
If you try to access an item with an index greater than the list index you will get an Index Error.
string_list Output: — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — - IndexError Traceback (most recent call last) <ipython-input-14–1d3b13a81e08> in <module> — → 1 string_list IndexError: list index out of range
Therefore from the above code samples, we understood how to append different types of data types to a list and access them.
Source: Towards Data Science
The Tech Platform