Join Two Lists
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the + operator.
Example
Join two list:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Try it Yourself »
Another way to join two lists are by appending all the items from list2 into list1, one by one:
Example
Append list2 into list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Try it Yourself »
Or you can use the extend() method, which purpose is to add elements from one list to another list:
Example
Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Try it Yourself »
Recipe Objective
Dataframes are data-objects in R which are combination of vectors of same length. It is represented as a two-dimensional array or a table where columns represent variables of the dataset while rows are the observations in it. Unlike matrices, dataframes contains different datatypes.
Often dataframes are created by loading a dataset from existing storage like an excel file, csv file. But we can also create a dataframe from vectors or lists in R. This recipe demonstrates how to create a dataframe combining 2 lists.
Table of Contents
- Recipe Objective
- Step 1: Creating 2 lists
- Step 2: Creating a Dataframe
Step 1: Creating 2 lists
We are going to take an example of student dataset which has variables like marks and name. To create this dataframe, we will first create 2 lists named "marks" and "name".
Note: the length of each lists has to be same
name = list('Tom', "Harry", "David", "Daniel") marks = list(50,60,35,95)
Step 2: Creating a Dataframe
We use data.frame() and unlist() functions to create a dataframe using lists. unlist() function is used to covert list to vector so that we can use it as "df" argument in data.frame() function.
Syntax:
1. data.frame(df, stringAsFactors)
where:
- df = is matrix or collection of vectors that needs to be joined;
- stringAsFactors = if TRUE, it converts string to vector by default;
unlist(x, recursive = TRUE, use.names = TRUE)
where:
- x = lists;
- recursive = By defalut it's TRUE but if FALSE, the function won't recurse beyond first level of list;
- use.names = By default it's TRUE and its meant to preserve the naming information;
student = data.frame(unlist(name),unlist(marks)) #to name the columns we use names() function names(student) = c("Name","Marks") student
Tom 50 Harry 60 David 35 Daniel 95You can use either of the following templates in order to concatenate two lists in Python:
(1) Using the + operator:
list_one = ['item1', 'item2', 'item3', ....] list_two = ['item1', 'item2', 'item3', ....] concatenated_list = list_one + list_two print(concatenated_list)(2) Using extend:
list_one = ['item1', 'item2', 'item3', ....] list_two = ['item1', 'item2', 'item3', ....] list_one.extend(list_two) print(list_one)In the next section, you’ll see how to apply the above techniques in practice.
Step 1: Create two Lists
To begin with a simple example, let’s create two lists that contain string values:
list_one = ['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear'] list_two = ['Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut'] print(list_one) print(list_two)Run the code in Python, and you’ll get these two lists:
['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear'] ['Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut']Step 2: Concatenate the two Python Lists using the + operator
You can use the + operator in order to concatenate the two lists:
list_one = ['item1', 'item2', 'item3', ....] list_two = ['item1', 'item2', 'item3', ....] concatenated_list = list_one + list_two print(concatenated_list)For our example:
list_one = ['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear'] list_two = ['Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut'] concatenated_list = list_one + list_two print(concatenated_list)As you can see, the two lists are now concatenated:
['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear', 'Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut']Similarly, you can use the + operator to concatenate two lists that contain integers:
list_one = [1, 2, 3, 4, 5] list_two = [6, 7, 8, 9, 10] concatenated_list = list_one + list_two print(concatenated_list)Here is the result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Using Extend
Alternatively, you can use extend to concatenate the two lists:
list_one = ['item1', 'item2', 'item3', ....] list_two = ['item1', 'item2', 'item3', ....] list_one.extend(list_two) print(list_one)Here is the complete Python code for our example:
list_one = ['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear'] list_two = ['Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut'] list_one.extend(list_two) print(list_one)Result:
['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear', 'Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut']Concatenate More Than Two Lists
You can use the + operator to concatenate multiple lists.
For example, let’s concatenate the following 4 lists:
list_one = ['Banana', 'Apple', 'Mango'] list_two = ['Watermelon', 'Pear'] list_three = ['Blueberry', 'Cherry'] list_four = ['Pineapple', 'Papaya', 'Coconut'] concatenated_list = list_one + list_two + list_three + list_four print(concatenated_list)Run the code, and you’ll get the following result:
['Banana', 'Apple', 'Mango', 'Watermelon', 'Pear', 'Blueberry', 'Cherry', 'Pineapple', 'Papaya', 'Coconut']Finally, you can check the following guide to learn more about creating a list in Python.