This article show how to add elements to list.
Contents
- append : Add an element at the end
- insert : Add element at specified position
- extend : Concatenate list at the end
- join : Concatenate elements of string
- "+" : Concatenate list
- "+=" : Add list
- Insert list at specified position using slice
sponsored link
append : Add an element at the end
By using append() method, an element is added to the end of the list.
List.append( element )
Here is an example code.
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
# ['Apple', 'Banana', 'Orange']
fruits.append("Grape")
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape']
If list is specified as argument, it is added as an element.
Note that this is not "concatenating".
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
# ['Apple', 'Banana', 'Orange']
num_list = [1, 2, 3]
fruits.append(num_list)
print(fruits)
# ['Apple', 'Banana', 'Orange', [1, 2, 3]]
sponsored link
insert : Add element at specified position
By using insert() method, an element is inserted at specified position in list.
List.insert( index, element )
Here is an example code.
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
# ['Apple', 'Banana', 'Orange']
fruits.insert(1, "Grape")
print(fruits)
# ['Apple', 'Grape', 'Banana', 'Orange']
Same as "append", if list is specified as argument, it is added as an element.
Note that this is not "concatenating".
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
# ['Apple', 'Banana', 'Orange']
num_list = [1, 2, 3]
fruits.insert(1, num_list)
print(fruits)
# ['Apple', [1, 2, 3], 'Banana', 'Orange']
sponsored link
extend : Concatenate list at the end
By using extend() method, a list is concatenated at the end of another list.
List.extend( List )
Here is an example code.
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
# ['Apple', 'Banana', 'Orange']
num_list = [1, 2, 3]
fruits.extend(num_list)
print(fruits)
# ['Apple', 'Banana', 'Orange', 1, 2, 3]
sponsored link
join : Concatenate elements of string
If list contains only elements of string, by using join() function, elements are concatenated into string.
' Delimiter '.join( List )
Here is an example code.
◆No delimiter
example_list = ['I', 'have', 'a', 'pen']
print(example_list)
# ['I', 'have', 'a', 'pen']
example = ''.join(example_list)
print(example)
# Ihaveapen
◆delimiter : ","
example_list = ['I', 'have', 'a', 'pen']
print(example_list)
# ['I', 'have', 'a', 'pen']
example = ' '.join(example_list)
print(example)
# I have a pen
In the following example code, list contains elements of data types other than string.
Before joining, all elements in the list must be converted to string type.
In the following example code, elements are converted to string type by using map function.
example_list = ["Apple", "Banana", "Orange", 100, 200, 300]
print(example_list)
# ['Apple', 'Banana', 'Orange', 100, 200, 300]
example_list_str = map(str, example_list)
example = ','.join(example_list_str)
print(example)
# Apple,Banana,Orange,100,200,300
sponsored link
"+" : Concatenate list
Also by using "+" operator, lists are combined.
List + List
Here is an example code.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Grape", "Mango", "Lemon"]
fruits_3 = fruits_1 + fruits_2
print(fruits_3)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon']
sponsored link
"+=" : Add list
The "+=" operator also combines lists, but rewrites the data in first list.
List += List
Here is an example code.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Grape", "Mango", "Lemon"]
fruits_1 += fruits_2
print(fruits_1)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon']
sponsored link
Insert list at specified position using slice
Also by using "Slice", a list is inserted at specified position in list
List[ index : index ] = List
Here is an example code.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Grape", "Mango", "Lemon"]
fruits_1[1:1] = fruits_2
print(fruits_1)
# ['Apple', 'Grape', 'Mango', 'Lemon', 'Banana', 'Orange']
By using "Slice", elements can also be replaced.
fruits_1 = ["Apple", "Banana", "Orange"]
fruits_2 = ["Grape", "Mango", "Lemon"]
fruits_1[1:2] = fruits_2
print(fruits_1)
# ['Apple', 'Grape', 'Mango', 'Lemon', 'Orange']
sponsored link