ADD LIST IN A LIST PYTHON code example

Example 1: python append vs extend

my_list = [23, 11, 42, 24523]

# append will add it as if you're adding a new list to it
my_list.append([34523, 76979])
print(my_list)

# extend will go over each item in the new source list and add each
# element as part of the target list (my_list)
my_list.extend([12, 99])
print(my_list)

""" 
Output:
[23, 11, 42, 24523, [34523, 76979]]
[23, 11, 42, 24523, [34523, 76979], 12, 99]
"""

Example 2: how to add an item to a list in python

myList = [1, 2, 3]

myList.append(4)

Example 3: how to add a new element to a list in python

#!/usr/bin/env python

# simple.py

nums = [1, 2, 3, 4, 5]

nums.append(6)

Example 4: how do i add to a list in python

a=[1,2,3,4]

a+=[5,6]

Example 5: how to add an item to a list python

numbers = [1, 2, 3, 4]
numbers.append(10)
print(numbers)

Example 6: how to add to a list python

a_list = [1,2,3]
a_list.append(4)

Tags: