How to Add Element at Beginning of List in Python?
Hi Dev,
If you need to see example of how to add element at beginning of list in python. This post will give you simple example of add element to list python at index 0. We will use how to add an element to the start of a list in python. you'll learn how to add an element to the beginning of a list in python. follow bellow step for python add element to list beginning.
There are many ways to add elements at the first of the list in python. i will give you two examples using insert() method and list key to add element to list python at index 0. so let's see the below examples.
so let's see following examples with output:
Example 1:
main.py
myList = [1, 2, 3, 4]
# Add new element at first
myList.insert(0, "First Elem")
print(myList)
Output:
['First Elem', 1, 2, 3, 4]
Example 2:
main.py
myList = [1, 2, 3, 4]
# Add new element at firdt
myList = ["First Elem"] + myList
print(myList)
Output:
['First Elem', 1, 2, 3, 4]
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?