Append a List to a List in PythonWhat is a List in Python?A List is a type of data that stores several elements. We can define a list using a variable, which is enclosed using a square bracket [ ]. The data items are separated by a comma. A list of fruits can be defined as: Output: The List is : ['Apple', 'Banana', 'Grapes', 'Watermelon'] What is the append( ) Function in Python?The append( ) function in Python adds elements to an existing list. It inserts the elements at the end of the list. This function adds the elements to the original list instead of creating a new list. As the element is added to the list using the append function, it takes the whole list as a single object, and the length of the list is increased by 1. Syntax of append( ) in Python where:
We can append an element or another list to a list using the append( ) function. It takes the object as a whole to append to the list. Let's understand the append( ) function to append a list to a list using some examples in Python. Examples:1. Appends a List of Integers to a List Code: Output: Appended List is: [1, 2, 3, 4, 5, [6, 3, 8, 9]] We append list2 to list1, that is, we appended the items of list2 at the end of the list1 using the append( ) function. As output, it first prints the contents of list1 and then adds the entire list2 as a single object to the end of list1. 2. Appending a List of String Data to an Integer List Code: Output: Appended List is: [1, 2, 3, 4, 5, ['abc', 'def', 'jkl']] We have declared a list (list1) containing different integers and another list of string data (list2). As an output, it gives list1 elements with list2 elements as a single element appended at the end of list1. 3. Check the Length of the Original and Appended Lists Code: Output: Length of the original list is: 5 Appended List is: [1, 2, 3, 4, 5, [6, 3, 8, 9, 10]] Length of the appended list is: 6 We have declared two lists containing different integers. We first checked the length of the list1 before appending it. It contains 5 elements; thus, the length of list1 is 5. Then, we appended the list1 with list2 and printed the length of the appended list. The list2 has five elements, but the length of the appended list is 6, as it takes list2 as a whole, which makes the length of appended list 6. Some Important Features of the append( ) Function in Python
We can append a list to another list using the extend( ) function in Python. What is the extend( ) Function in Python?The extend( ) function in Python adds the elements to a list at the end. It adds each element to the list, in contrast to the append( ) function, which takes the input as a whole. It does not make a copy of the list but appends each record to the original list. It takes iterables as input, which includes lists, tuples, arrays, etc. The extend( ) function iterates each element of the list, which extends the list by the number of items added. Syntax of extend( ) in Python where:
Let's understand the extend( ) function to append a list to a list with some examples in Python. Examples:1. Append a List to Another List Code: Output: Appended List is: [9, 8, 7, 6, 5, 10, 11, 23, 45, 56] We have taken two lists of numbers. Using the extend( ) function, we appended list1 to list2. It adds the elements of the list1 at the end of the list2. It iterates each list element individually and appends the data to the other list. 2. Check the Length of the Original and Appended Lists. Code: Output: Length of the original list is: 5 Appended List is: [21, 34, 44, 1, 'q', 6, 3, 8, 9, 0, 10] Length of the appended list is: 11 We have taken two lists. The first list contains 5 elements of integers and string elements. The second list contains 6 elements of integer value. We append the list2 to list1 using the extend function. The elements of the list2 are added at the end of the list1. The length of the appended list is 11, as the extend( ) function adds the element individually. Some Important Features of the extend( ) Function in Python
Next TopicAppend-csv-files-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India