Concatenation makes it easy to create a new list starting with an integer
You can create a new list by concatenating an integer (wrapped in a list) with an existing list
my_integer = 5
my_list = [1, 2, 3, 4]
# Create a new list
new_list = [my_integer] + my_list
print(new_list) # Output: [5, 1, 2, 3, 4]
Using list comprehension
Or you can do this: Use list comprehension:
my_integer = 5
my_list = [1, 2, 3, 4]
# Create a new list
new_list = [my_integer] + [item of item in my_list]
print(new_list ) # Output: [5, 1, 2, 3, 4]
Using 'append'
Using 'append', you can start with an empty list and append integers and elements to an existing list:
my_integer = 5
my_list = [1, 2, 3, 4]
# Start with an empty list
new_list = []
new_list.append(my_integer) # Add an integer
# Expand in the original list
new_list.extend (my_list)
print(new_list) # Output: [5, 1, 2, 3, 4]