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.
Creation :
In Python, lists are created using the square brackets and each item inside a list is separated by commas.
Code:
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.
Code:
len(my_list)
Output:
6
We can also check each item value based on its index as below.
Code:
my_list[0],my_list[3]
Output:
(‘I’, ‘is’)
Till now we discussed on Collection type List now let’s move on to the append method in Python.
Append Method
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.
Syntax
list_name.append(item)
Parameters
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.
Return Value
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.
Code:
# 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
Code:
#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.
Code:
string_list[5]
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.
Code:
string_list[5][1]
Output:
2
If you try to access an item with an index greater than the list index you will get an Index Error.
Code:
string_list[6]
Output:
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -
IndexError Traceback (most recent call last)
<ipython-input-14–1d3b13a81e08> in <module>
— → 1 string_list[6]
IndexError: list index out of range
Conclusion :
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
Comentarios