Adding Items to a List
Listโs versatility to comes from its ability to contain information and add on information. This lesson covers how to do so.
Adding an Item to the End of a List
This is the safest and the recommended way to add values: add the new value to the end.
.append(item)
is method that adds the given item to the end of a list.
# .append() Example
a_list = ['apple', 'oranges']
a_list.append('kiwi')
a_list.append('honeydew')
print('a_list:', a_list)
a_list: ['apple', 'oranges', 'kiwi', 'honeydew']
Adding an Item at a Location
This technique can cause potential issues; however, it does exist!
.insert(location, item)
will add the given item at the specified integer location.
NOTES:
If the location is beyond the size, it will add it to the end
Location can be negative
If the location already has a value, it will shift everything at the location onwards to the right to add the item
# .insert() Example
a_list = [2,3,5,7]
a_list.insert(5, 11)
print('a_list.insert(5, 11):', a_list)
a_list.insert(-1, 13)
print('a_list.insert(-1, 13):', a_list)
a_list.insert(2, 4)
print('a_list.insert(2, 4):', a_list)
a_list.insert(5, 11): [2, 3, 5, 7, 11]
a_list.insert(-1, 13): [2, 3, 5, 7, 13, 11]
a_list.insert(2, 4): [2, 3, 4, 5, 7, 13, 11]
Combining Lists
There are multiple ways to combine lists!
Manipulate a list with all the value from another at the tail-end.
.extend(list)
is a method that adds all the items from the given list argument to its list.
# .extend() example
basket = [] # Empty List
fruits = ['apple', 'oranges', 'kiwi', 'honeydew']
vegetables = ['onions', 'garlic', 'spinach']
basket.extend(fruits)
basket.extend(vegetables)
print('basket:', basket)
print('fruits:', fruits)
print('vegetables:', vegetables)
basket: ['apple', 'oranges', 'kiwi', 'honeydew', 'onions', 'garlic', 'spinach']
fruits: ['apple', 'oranges', 'kiwi', 'honeydew']
vegetables: ['onions', 'garlic', 'spinach']
Use the
+ operator
to generate a new list
The +
operator is the concatenation operator much like how strings have one.
Instead of manipulate the left operand, it will return a new list.
# concatenation example
fruits = ['apple', 'oranges', 'kiwi', 'honeydew']
vegetables = ['onions', 'garlic', 'spinach']
baskets = fruits + vegetables
print('basket:', basket)
basket: ['apple', 'oranges', 'kiwi', 'honeydew', 'onions', 'garlic', 'spinach']
Preserve the variable but generate a new list
+=
operator
We can also use the +=
assignment operator to concatenate then assign.
# += example
primes = [2, 3, 5, 7]
primes += [11, 13]
print('primes:', primes)
primes: [2, 3, 5, 7, 11, 13]
List Repetition Operation
*
operator helps us repeat a list.
Example:
a = [1,2,3]
print(a*3)
a *= 3
print(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Last updated