{"id":3458,"date":"2021-08-24T18:45:34","date_gmt":"2021-08-24T13:15:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=3458"},"modified":"2021-11-22T18:39:33","modified_gmt":"2021-11-22T13:09:33","slug":"python-how-to-create-an-empty-list-and-append-items-to-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-create-an-empty-list-and-append-items-to-it\/","title":{"rendered":"Python: How to Create an Empty List and Append Items to it?"},"content":{"rendered":"
An ordered list of values is a collection. There may be different types of values. A list is a container that is mutable. This means that the existing ones can be added, deleted, or modified.<\/p>\n
The list of Pythons represents a finite sequence mathematical concept. List values are called list items or list elements. A list may multiple times contain the same value. Each event is considered to be a separate element.<\/p>\n
In this article, we will look at how to create an empty list and add elements to the list in Python.<\/p>\n
There are several ways to create and append values to list some of them are:<\/p>\n
Creating a list:<\/strong><\/p>\n Adding elements to list:<\/strong><\/p>\n An empty list can be generated in Python by simply writing square brackets, i.e. []. If there are no arguments in the square brackets [, it returns an empty list.<\/p>\n <\/p>\n Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The list class in Python has a constructor<\/p>\n It takes an optional statement, which is an iterable sequence, and constructs a list from these elements. It returns an empty list if Sequence is not given. Let’s use this to make a blank list.<\/p>\n <\/p>\n Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The built-in append() function can be used to add elements to the List. The append() method can only add one element to the list at a time; loops are used to add several elements to the list with the append() method. Since tuples are immutable, they can also be added to the list using the append method. Lists, unlike Sets, can be appended to an existing list using the append() form.<\/p>\n <\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n insert():<\/strong><\/p>\n It inserts the item in the list at the given index.<\/p>\n The append() method only adds elements to the end of the List; the insert() method is used to add elements to the desired location. Unlike append(), which only needs one statement, insert() requires two (index,element).<\/p>\n <\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n There is one more method to add elements other than the append() and insert() methods, extend(), used at the end of the list to add multiple elements simultaneously.<\/p>\n Note: the methods append() and extend() can only add elements at the end.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n An ordered list of values is a collection. There may be different types of values. A list is a container that is mutable. This means that the existing ones can be added, deleted, or modified. The list of Pythons represents a finite sequence mathematical concept. List values are called list items or list elements. A …<\/p>\n\n
\n
Creating a list<\/h3>\n
Method #1: Using [] symbol create an empty list in python<\/h4>\n
# creating new list\r\nnewlist = []\r\n# printing it\r\nprint(\"Newlist = \", newlist)\r\n<\/pre>\n
Newlist = []<\/pre>\n
Method #2: Using list() Constructor create an empty list in python<\/h4>\n
list( [iterable] ).<\/code><\/p>\n
# creating new list\r\nnewlist = list()\r\n# printing it\r\nprint(\"Newlist = \", newlist)\r\n<\/pre>\n
Newlist = []<\/pre>\n
Adding items\/elements to the list<\/h3>\n
Method #1: Using append() function add items to an empty list in python<\/h4>\n
# creating new empty list\r\nnewlist = list()\r\n# adding items 1 2 3 to it\r\nnewlist.append(1)\r\nnewlist.append(2)\r\nnewlist.append(3)\r\n# printing it\r\nprint(\"Newlist = \", newlist)\r\n<\/pre>\n
Newlist = [1, 2, 3]<\/pre>\n
Method #2:Using insert() function add elements to an empty list in python<\/h4>\n
list.insert(index,element)<\/code><\/p>\n
# Taking a list with values\r\nnewlist = ['this', 'is', 'BTechGeeks']\r\n# inserting element hello to 2 nd index using insert()\r\nnewlist.insert(2, 'hello')\r\n# printing the list\r\nprint(\"list = \", newlist)\r\n<\/pre>\n
list = ['this', 'is', 'hello', 'BTechGeeks']<\/pre>\n
Method #3:Using extend() function add items to an empty list in python<\/h4>\n
# Taking a list with values\r\nnewlist = ['this', 'is', 'BTechGeeks']\r\n# extend this newlist with multiple items\r\nnewlist.extend(['hello', 'python', 234])\r\n# printing the list\r\nprint(\"list = \", newlist)\r\n<\/pre>\n
list = ['this', 'is', 'BTechGeeks', 'hello', 'python', 234]<\/pre>\n
\n