Lists

Appending Lists

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.

Syntax:

# Adds an object (a number, a string or a

# another list) at the end of my_list

my_list.append(object)

appending

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.

Creation

In Python, lists get created by making use of the square brackets. Similarly, each item inside the list separates by commas.

Code:

                    

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

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.

Code:

One can also check the value of each item on the basis of its index as follows:

Code:

                    

my_list[0],my_list[3]

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.

Append Method

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.

Syntax

list_name.append(item)

Parameters

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.

Return Value

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.

  1. Adding Number to a List:

In the code below, we will learn 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’]

  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:

Code:

                    

#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.

Code:

                    

string_list[5]

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.

Code:

                    

string_list[5][1]

Output:

2

If one tries to access an item with an index greater than the list index, one will get an Index Error.

Code:

                    

string_list[6]

Output:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

IndexError Traceback (most recent call last)

<ipython-input-14–1d3b13a81e08> in 

— → 1 string_list[6]

IndexError: list index out of range

Conclusion

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.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.