snippetpythonCritical
How do I concatenate two lists in Python?
Viewed 0 times
howpythonliststwoconcatenate
Problem
How do I concatenate two lists in Python?
Example:
Expected outcome:
Example:
listone = [1, 2, 3]
listtwo = [4, 5, 6]Expected outcome:
>>> joinedlist
[1, 2, 3, 4, 5, 6]Solution
Use the
Output:
NOTE: This will create a new list with a shallow copy of the items in the first list, followed by a shallow copy of the items in the second list. Use copy.deepcopy() to get deep copies of lists.
+ operator to combine the lists:listone = [1, 2, 3]
listtwo = [4, 5, 6]
joinedlist = listone + listtwoOutput:
>>> joinedlist
[1, 2, 3, 4, 5, 6]NOTE: This will create a new list with a shallow copy of the items in the first list, followed by a shallow copy of the items in the second list. Use copy.deepcopy() to get deep copies of lists.
Code Snippets
listone = [1, 2, 3]
listtwo = [4, 5, 6]
joinedlist = listone + listtwo>>> joinedlist
[1, 2, 3, 4, 5, 6]Context
Stack Overflow Q#1720421, score: 5635
Revisions (0)
No revisions yet.