python combine two lists -different length-without duplicate

python combine two lists :

Hello everyone, welcome to my python Tutorials point. In this post we can see different methods to show How python combine two lists:
Before going to this post if you want see my tutorials post then click Here;
Let’s see how to combine two list with different methods. This operation is very useful when we have number of list of element which needs to be processed in a similar manners.
Different Methods to combine two lists:
1. Using Native Method
2. Using + operator
3. Using list comprehension
4. Using extend()
5.Using * operator
python combine two lists -different length-
fig 01)python combine two lists -different length-without duplicate


1 Method : python combine two lists Using Naive Method
In this methods, the second lists and keep appending element in the first lists, so that first lists would have all the element in both list and hence would performs the append.
# python code for combine two list
# with the help of native method

# Initializing list 
test_list1 = [1, 3, 5, 7, 9] 
test_list2 = [2, 4, 6, 8, 10] 

# using naive methods to combine 
for i in test_list2 : 
 test_list1.append(i) 

# Printing combine list 
print ("Combine list using naive method : "
      + str(test_list1)) 
OUTPUT:
Combine list using naive method : [1,3,5,7,9,2,4,6,8,10]

Method  : python combine two list Using + operator

The most simple method to perform the combine two list, the use of “+” operators can easily add the whole of one lists behind the other list and hence perform the operation.
# python code for combine two list
# with the help of + operator
# Initializing lists
test_list1 = [1, 3, 5, 7, 9] 
test_list2 = [2, 4, 6, 8, 10] 
# using + operator to combine list test_list1 = test_list1 + test_list2 # Printing combine list print ("Combine list using + : " + str(test_list3))
OUTPUT:
Combine list using + : [1,3,5,7,9,2,4,6,8,10]

Method : python combine two list Using list comprehension

List comprehension can also accomplish this task of combine lists. In this method, a new list is created, but this method is a one liner alternative to the loop methods discussed  in above.
# python code for combine two list
# with the help of 
# Initializing lists test_list1 = [1, 3, 5, 7, 9] test_list2 = [2, 4, 6, 8, 10] # using list comprehension to combine list com_list = [y for x in [test_list1, test_list2] for y in x] # Printing concatenated lists print ("Combine list using list comprehension: " + str(com_list))
OUTPUT:
Combine list using list comprehension: [1,3,5,7,9,2,4,6,8,10]


Method  : python combine two list Using extend()

extend() is the functions extended by list in Python and hence can be used to perform this  combine task. This function perform the inplaced extension of first lists.
# Python3 code to combine list
#using list.extend() method

# Initializing list 
test_list1 = [1,3,5,7,9] 
test_list2 = [2,4,6,8,10] 

# using list.extend() to concat 
testing_list1.extend(test_list2) 

# Printing combine lists 
print ("Combined list using list.extend() : "
      + str(testing_list1)) 
OUTPUT:
Combined list using list.extend() : [1,3,5,7,9,2,4,6,8,10]
Method  : python combine two list Using * operator
Using * operators, this method is the new addition to combined list.and work only in Python 3.6 remember it. Any number of list can be combined and returned in a new list using this operators.
# Python code to combined list
# combined list using * operator 

# Initializing lists 
test_list1 = [1, 3, 5, 7, 9] 
test_list2 = [2, 4, 6, 8, 10] 

# using * operator to combined list
test_list = [*test_list1, *test_list2] 

# Printing combined list 
print ("Combined list using * operator : "
       + str(test_list)) 
OUTPUT:
Combined list using * operator : [1,3,5,7,9,2,4,6,8,10]
Tags: python combine Two list, dictionary, different length, without duplicate, vertically,element wise
FOR SIMILAR POST CLICK HERE AND IF YOU LIKE THIS POST THEN PLEASE COMMENT AND SHARE.
                                                              BEST OF LUCK!!!!
Previous
Next Post »

Featured Snippet

10 ways to iterate through a list in python

10 ways to iterate through a list in python The list is similar to array in other languages except for python, which provides the extra b...