Append list to list Python

Startup Business Insurance Needs to Be Taken Seriously

In Python, lists are one of the most helpful ways to organize your data set. Learn how to create lists and append items.
Sunil Kumar
Expert Contributor

MLOps engineer at Deloitte.

Updated:
Sunil Kumar
Expert Contributor

MLOps engineer at Deloitte.

Updated:
Join the Expert Contributor Network
Join the Expert Contributor Network

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 dont need to have the same data type. Another major advantage is that lists are mutable, which means we can change a list even after we create it.

Creating Lists in Python

First, lets talk about how to create a list. In Python, we create lists using square 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 Ive added both string and numeric data types inside the same list.

Indexing Lists

Lists in Python are indexed and have a defined count. The elements in a list are likewise indexed according to a defined sequence with 0 being the first item and n-1 being the last [n is the number of items in a list]. Each item in the list has its indexed place.

Our list index starts with 0 and ends with 5, which we can check using the pre-built len[] function:

len[my_list] Output: 6

We can also check each item value 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

Append Method

The append[] method in Python adds a single item to the end of the existing list.

After appending to 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.

Items you can Append in a Python List

  • Numbers
  • Strings
  • Boolean data types
  • Dictionaries
  • Lists

Adding Numbers to a List

Lets look at how to add a new numeric item to a list:

# list of strings string_list = [Built In,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: [Built In,Python,Machine Learning,Data Science,1]

Adding a List to a List

In addition to adding a strings or numeric data types, we can also append 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 appended list string_list Output: [Built In,Python,Machine Learning,Data Science,1,[1,2,3,4,5]]

You can see from the output that a new list is appended at the end of our old list.

To get the whole list as an indexed item, well use:

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.

string_list[5][1] Output: 2

If you try to access an item with an index greater than the list index youll get an index error.

string_list[6] Output: - IndexError Traceback [most recent call last] in 1 string_list[6] IndexError: list index out of range

With these code samples, we can append different data types to a list and access them with ease. This is just one of the many methods we can use in Python lists that make life easier for any Python developer.

This post was originally published on Towards Data Science.

Data Science
Expert Contributors
Software Engineering Perspectives

Video liên quan

Chủ Đề