[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']
This example added a list of [1, 2]
. Then it added a tuple of (3, 4)
. And then it added a string of ABC
.
If you have to concatenate multiple lists, you can use the +
operator. This will create a new list, and the original lists will remain unchanged.
evens = [2, 4, 6]
odds = [1, 3, 5]
nums = odds + evens
print(nums)
This example added the list of evens
to the end of the list of odds
. The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.
Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded +
operator
References:
Python List
Python.org Docs
JournalDev
DigitalOcean Employee
•
December 6, 2020
list1 = [“M”, “na”, “i”, “Ke”] list2 = [“y”, “me”, “s”, “lly”] list3 = [i + j for i,j in zip(list1, list2)] How is the above different to this; list3 = list() for i,j in zip(list1, list2): list3 = i + j how are these 2 codes different from one another?
- Asad Jaffer
JournalDev
DigitalOcean Employee
•
November 23, 2020
how to use add method like i mean list.add(x)… explain