DEV Community

BigdataWorld
BigdataWorld

Posted on

Append Python

While picking a collection type, it is helpful to comprehend the properties of each type and picking the most suitable type for a specific informational index. To realize the most suitable collection type you need to know the qualities of the multitude of accessible types and afterward pick one from it dependent on your utilization case. In this article, we will experience the List Collection Type alongside the append technique in the article.

Lists are like the exhibits, proclaimed in different dialects. Lists need not be of a similar information types consistently which makes it a most amazing asset in Python and is the fundamental contrast among exhibits and lists. A list can contain information Types, for example, Integers, Strings, just as lists. Lists are variable, which implies they can be adjusted even after their creation.

List in Python are indexed and have a positive check while instating. The components in elite are indexed by a clear grouping and the indexing of a list is finished with 0 being the main index and the keep going thing index is n-1 where n is the quantity of things in top notch. Every component in the list has its indexed place in the list, which permits copying of components in the list i.e we can make a similar indexed thing as another with an alternate index and the list will at present acknowledge it, in contrast to 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 stringsstring_list = [‘Medium’,’Python’,’Machine Learning’,’Data Science’]#adding a new int item to the string_liststring_list.append(1)#printing appended listprint(string_list)Output:[‘Medium’,’Python’,’Machine Learning’,’Data Science’,’1’]

  1. 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 listnew_list = [1,2,3,4,5]#append this list to our string_liststring_list.append(new_list)# print the appended liststring_listOutput:[‘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) in — → 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.

We have many such methods in Python Lists apart from the append which will make life easier for a Python developer.

Source : https://bigdata-world.net/
https://bigdata-world.net/append-python/

Top comments (0)