How to Append Lists in Python – Built In

There are many advantages to using lists in Python. Although lists are similar to arrays in other languages, the main difference between lists and arrays is that lists do not need to have the same type of data. Another important advantage is that lists are mutable, which means that we can change a list even after creating it.

Clean up your code5 ways to write more

pythonic code

Create lists in Python

First, let’s talk about how to create a list. In Python, we create lists using brackets. We separate each item in the list with a comma.

my_list = [‘I’,’think’,’Built-In’,’is’,’number’,’1′] type(my_list) Output: list

You can see that I have added numeric and string data types within the same

list.

Learn Python programming – 13 – Attached list method. | Video: Indexing lists from smart programmers

in Python Lists

in Python

are indexed and have a defined count. Items in a list are also indexed according to a defined sequence where 0 is the first item and n-1 is the last (n is the number of items in a list). Each item in the list has its place indexed.

More Python tutorials on BuiltInHow to write nested list comprehensions in Python

Our list index starts with 0 and ends with 5, which we can check using the prebuilt

len() function:len(my_list) Output: 6

We can also check the value of each element based on its index

:my_list[0],my_list[3] Output: (‘I’, ‘is’)

Read more about Python 5 Dunder Methods in Python You should know

how to add lists

in Python The append() method in Python adds a single item to the end of the existing list. After you append to the list

, the size of the list increases by one

. Append

Syntax

list_name.append(item)

Append Parameters

The append() method takes a single item as an input parameter and adds it to the end of the list.

Add numbers to a list with append

Let’s see how to add

a new numeric element to a list:# string list string_list = [‘Built-In’,’Python’,’Machine Learning’,’Data Science’] #adding a new int element to the list string_list string_list.append(1) #printing appended print(string_list) Output: [‘Built-In’,’Python’,’Machine Learning’, ‘Data Science’,’1′] Add a list to a list

with append

In addition to adding

strings or numeric data types, we can also add separate lists

to a list:#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 list appended string_list Output: [‘Built-In’,’Python’,’Machine Learning’,’Data Science’,’1′,[1,2,3,4,5]]

You can see in the output that a new list is added at the end of our previous list.

To get the entire list as an indexed item, we will use

:string_list[5] Output: [1,2,3,4,5] If you want to access the items in this list, you

can do so in the same way you access the items in a 2D array

. string_list[5][1] Result: 2

If you try to access an item with an index larger than the list index, you will get an index error.

string_list[6] Output: — — <— — — →><>— — — string_list— —

This is just one of the many methods we can use in Python lists that make life easier for any Python developer.

Contact US