Home//

Python List Append Items Tutorial

Python List Append Items Tutorial

Minh Vu

By Minh Vu

Updated Oct 13, 2023

What is Append in Python?

In Python, append means to add an item to the end of a list.

You can use the append() method to append items to a list in Python.

To append a list to another list, use the extend() method or simply use the + operator for list concatenation.

Append Single Item to List

To append a single item to the list, use the append() method with one argument which is the item to be appended. Here's the syntax:

list_name.append(value)

Let's look at some examples:

  • Append a string to a list:

    name_list = [] name = "Minh Vu" name_list.append(name) print(name_list) # ['Minh Vu'] name = "Nhi Pham" name_list.append(name) print(name_list) # ['Minh Vu', 'Nhi Pham']
  • Append an integer to a list:

    number_list = [] number = 1 number_list.append(number) print(number_list) # [1] number = 2 number_list.append(number) print(number_list) # [1, 2]
  • Append a float to a list:

    number_list = [] number = 1.5 number_list.append(number) print(number_list) # [1.5] number = 2.5 number_list.append(number) print(number_list) # [1.5, 2.5]

Append a List to Another List

To append a list to another list, you use the extend() method.

name_list = ["Minh Vu"] another_name_list = ["Nhi Pham"] name_list.extend(another_name_list) print(name_list) # ['Minh Vu', 'Nhi Pham']

Or simply use the + operator for list concatenation:

name_list = ["Minh Vu", "Nhi Pham"] another_list = ["WiseCode Blog"] name_list += another_list print(name_list) # ['Minh Vu', 'Nhi Pham', 'WiseCode Blog']

One common mistake I usually get is to use the append() method for this case, but remember that it will append the whole list as an item to the list, which is not the thing we want.

name_list = ["Minh Vu"] another_name_list = ["Nhi Pham"] name_list.append(another_name_list) print(name_list) # ['Minh Vu', ['Nhi Pham']]

Append Multiple Items to List

Similarly, if you want to append mutiple items to the list simultaneously, put them inside a list and use the extend() method like above.

url_list = [] url1 = "wisecode.blog" url2 = "formularizer.com" url_list.extend([url1, url2]) print(url_list) # ['wisecode.blog', 'formularizer.com']

Append Set to List

Similarly, to append a set to a list, you can use the extend() method.

name_list = ["Minh Vu"] name_set = {"Nhi Pham"} name_list.extend(name_set) print(name_list) # ['Minh Vu', 'Nhi Pham']

Conclusion

That's all. Let's recap this tutorial:

  • To append a single item to a list, use the append() method.
  • To append a list to another list, use the extend() method or the + operator for list concatenation.
  • To append multiple items to a list, put them inside a list and use the extend() method.
  • To append a set to a list, use the extend() method.

Thanks for reading!

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!