In Python there are several techniques to join lists and one of the easiest ways are by using the + operator.
mlist1 = ["a", "b", "c"]
mlist2 = [1, 2, 3]
mlist3 = mlist1 + mlist2
Now mlist3 would be ['a', 'b', 'c', 1, 2, 3]
This '+' operator creates a new list and store the values of the lists. We can join several lists using + operator.
The other technique to join two lists are by appending all the items from one list into another list, one by one. No extra list will be created here. This is carried out using append () method.
Append mlist2 into mlist1:
mlist1 = ["a", "b" , "c"]
mlist2 = [1, 2, 3]
mlist1.append(mlist2)
Now mlist1 would be ['a', 'b', 'c', 1, 2, 3]
extend() method can do the same job as append () method.
mlist1 = ["a", "b" , "c"]
mlist2 = [1, 2, 3]
mlist1.extend(mlist2)
Now mlist1 would be ['a', 'b', 'c', 1, 2, 3]
No comments:
Post a Comment