Appending basically refers to adding its arguments as a single element to the end of a list. Moreover, the length of the list keeps increasing by one.
# Adds an object (a number, a string or a
# another list) at the end of my_list
Importance of Appending
When you choose a collection type, it is essential to understand the properties of each type and to choose the most appropriate kind for a specific data set. In order to know the most appropriate collection kind, one needs to learn about the attributes of all the available types. After that, make a choice from all of the on the basis of your use case.
This article will help you understand the list collection type as well as learn about the appending method clearly. You know by now that lists are similar to the arrays, declared in another language.
They do not have to be of the same data types always. Thus, it makes them the most powerful tool in Python. Moreover, it is the most crucial difference between arrays and lists. In other words, a list may contain data types like integers, strings and lists.
Further, lists are mutable which means one can alter them even after they are created. In Python, lists are indexed and possess a definite count while initializing. The elements in a list are indexed as per a definite sequence and the indexing of a list happens with 0 as the first index and the last item index is n-1/
Over here, n is the number of items in a list. Each element in the list consists of its indexed place in the list. Thus, it allows the duplication of elements in the list. Meaning to say, one can create the same indexed item as another one with a totally different index and the list will still accept it which does not happen with sets.
In Python, lists get created by making use of the square brackets. Similarly, each item inside the list separates by commas.
my_list = [‘I’ ,”think” ,”Medium” ,’is’ , ‘number’ ,1] type(my_list) Output: list
From the above code, you will notice that when creating a list, both the string and numeric datatype have been given as items inside a list.
Browse more Topics under Lists
- Creating Lists
- Initializing and Accessing the Elements
- Traversing Lists
- Updating and Deleting Elements
- Lists as Arguments
- List Functions and Methods
Indexing in Lists
The list index begins with 0 and ends with n-1.
In this list, the index starts with 0 and ends with 5 which one can check by making use of the pre-built len() function.
len(my_list) Output: 6
One can also check the value of each item on the basis of its index as follows:
my_list,my_list Output: (‘I’, ‘is’)
Up until now, you have learned about the collection type list so now we will move on to the append method in Python.
The append() method in python will add a single item to the existing list. It does not return a new list of items. However, it will modify the original list when it adds the item to the end of the list.
After you execute the method append on the list, the size of the list will increase by one.
The append() method will take a single item as an input parameter and will add that to the end of the list. Thus, the items inside a list can consist of numbers, strings, another list, and a dictionary.
The append() method will simply modify the original list. It does not return any value as a return but will simply modify the created list.
- Adding Number to a List:
In the code below, we will learn 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)
[‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’]
- Adding new list to a List:
In addition to adding a string and a numeric data type, one can also add a separate list to a list as below:
#lets create a new list new_list = [2, 4, 6, 8, 10] #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’,[ 2, 4, 6, 8, 10]]
Thus, the above output shows you that a new list is appended at the end of our old list. We will get the whole list as an indexed item by making use of the below code.
string_list Output: [1, 2, 3, 4, 5]
If one wants to access the elements from this list, they can do that in the same way as one accesses element from a 2-D Matrix.
string_list Output: 2
If one tries to access an item with an index greater than the list index, one will get an Index Error.
string_list Output: — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — - IndexError Traceback (most recent call last) <ipython-input-14–1d3b13a81e08> in — → 1 string_list IndexError: list index out of range
Finally, from the code samples given above, you now know how to append different kinds of data types to a list and access them. We have many such methods in Python Lists apart from appending which will make it easier to learn about Python development.
FAQ on Appending Lists
Question 1: What does append () do in Python?
Answer 1: The append () method in python adds a single item to the existing list. It does not return a new list of items. However, it will modify the original list by adding the item to the end of the list.
Question 2: How do you create an empty list in Python?
Answer 2: In order to declare an empty list, you simply have to assign a variable with square brackets. The list () constructor will come into use for creating a list in Python. Parameters: iterable: This is an optional argument that can be a sequence (string, tuple) or collection (dictionary, set) or an iterator object.
Question 3: How do Python lists work?
Answer 3: A list refers to a data structure in Python. Moreover, it is a mutable, or changeable, ordered sequence of elements. Each element or value contains inside a list is referred to as an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].
Question 4: What is a list of lists called?
Answer 4: A list that contains other lists embedded in it may be referred to as a superlist.