Can append function Add item to the beginning of the list?

Python | Perform append at beginning of list

The usual append operation of Python list adds the new element at the end of the list. But in certain situations, we need to append each element we add in front of list. If we perform brute force techniques, we need to perform unnecessary shifts of elements and hence, having shorthands for it is useful.

Let’s discuss certain ways to perform append at beginning of the list.

Method #1 : Using insert()

This method generally inserts the element at any position in the list and also performs the necessary shifts required internally and hence can also be used to perform this very task.




# Python3 code to demonstrate
# to add element at beginning
# using insert()
# initializing list
test_list = [1, 3, 4, 5, 7]
# printing initial list
print ("Original list : " + str(test_list))
# using insert() to append
# at beginning. append 6
test_list.insert(0, 6)
# printing resultant list
print ("Resultant list is : " + str(test_list))
Output:

Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]


Method #2 : Using [] and +

These both operators can be combined to perform this task. We convert the element to list and then perform the list addition.




# Python3 code to demonstrate
# to add element at beginning
# using [] and +
# initializing list
test_list = [1, 3, 4, 5, 7]
# printing initial list
print ("Original list : " + str(test_list))
# using [] and + to append
# at beginning append 6
test_list = [6] + test_list
# printing resultant list
print ("Resultant list is : " + str(test_list))
Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]


Method #3 : Using Slicing

Slicing of list is also another method to perform this particular task. we just assign to the 0 sliced list to the list converted from the element. This does the trick and is quite elegant.




# Python3 code to demonstrate
# to add element at beginning
# using slicing
# initializing list
test_list = [1, 3, 4, 5, 7]
# printing initial list
print ("Original list : " + str(test_list))
# using slicing to append
# at beginning. append 6
test_list[:0] = [6]
# printing resultant list
print ("Resultant list is : " + str(test_list))
Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]


Method #4 : Using collections.deque.appendleft()

The list can be converted to deque and then the appendleft() can be used to perform the push like operation from the front of the doubly ended queue.




# Python3 code to demonstrate
# to add element at beginning
# using collections.deque.pushleft()
from collections import deque
# initializing list
test_list = [1, 3, 4, 5, 7]
# printing initial list
print ("Original list : " + str(test_list))
# using collections.deque.pushleft()
# to append at beginning
# append 6
test_list = deque(test_list)
test_list.appendleft(6)
test_list = list(test_list)
# printing resultant list
print ("Resultant list is : " + str(test_list))
Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]

Can append function Add item to the beginning of the list?




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

Append to Front of a List in Python

Python Python List

Created: February-10, 2021

This tutorial will demonstrate different ways on how to append an element to the front of a list in Python.

Throughout the tutorial, a list of integers will be used as examples to focus on list insertion instead of inserting various data types since the list insertion approach should be the same regardless of what data type the list contains.

Python's .append(): Add Items to Your Lists in Place

by Leodanis Pozo Ramos basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Building Lists With Python's .append()

Adding items to a list is a fairly common task in Python, so the language provides a bunch of methods and operators that can help you out with this operation. One of those methods is .append(). With .append(), you can add items to the end of an existing list object. You can also use .append() in a for loop to populate lists programmatically.

In this tutorial, you’ll learn how to:

  • Work with .append()
  • Populate lists using .append() and a for loop
  • Replace .append() with list comprehensions
  • Work with .append() in array.array() and collections.deque()

You’ll also code some examples of how to use .append() in practice. With this knowledge, you’ll be able to effectively use .append() in your programs.

Free Download: Get a sample chapter from Python Basics: A Practical Introduction to Python 3 to see how you can go from beginner to intermediate in Python with a complete curriculum, up-to-date for Python 3.8.

Add an item to a list in Python (append, extend, insert)

Posted: 2019-05-29 / Modified: 2021-04-06 / Tags: Python, List
Tweet

In Python, use list methods append(), extend(), and insert() to add items (elements) to a list or combine other lists. You can also use the + operator to combine lists, or use slices to insert items at specific positions.

  • Add an item to the end: append()
  • Combine lists: extend(), + operator
  • Insert an item at specified index: insert()
  • Add another list or tuple at specified index: slice
Sponsored Link

What are lists in Python? A definition for beginners

An array in programming is an ordered collection of items, and all items need to be of the same data type.

However, unlike other programming languages, arrays aren't a built-in data structure in Python. Instead of traditional arrays, Python uses lists.

Lists are essentially dynamic arrays and are one of the most common and powerful data structures in Python.

You can think of them as ordered containers. They store and organize similar kind of related data together.

The elements stored in a list can be of any data type.

There can be lists of integers (whole numbers), lists of floats (floating point numbers), lists of strings (text), and lists of any other built-in Python data type.

Although it is possible for lists to hold items of only the same data type, they are more flexible than traditional arrays. This means that there can be a variety of different data types inside the same list.

Lists have 0 or more items, meaning there can also be empty lists. Inside a list there can also be duplicate values.

Values are separated by a comma and enclosed in square brackets, [].

How to create lists in Python

To create a new list, first give the list a name. Then add the assignment operator(=) and a pair of opening and closing square brackets. Inside the brackets add the values you want the list to contain.

#create a new list of names names = ["Jimmy", "Timmy", "Kenny", "Lenny"] #print the list to the console print(names) #output #['Jimmy', 'Timmy', 'Kenny', 'Lenny']

How lists are indexed in Python

Lists maintain an order for each item.

Each item in the collection has an its own index number, which you can use to access the item itself.

Indexes in Python (and every other modern programming language) start at 0 and increase for every item in the list.

For example, the list created earlier on had 4 values:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

The first value in the list, "Jimmy", has an index of 0.

The second value in the list, "Timmy", has an index of 1.

The third value in the list, "Kenny", has an index of 2.

The fourth value in the list, "Lenny", has an index of 3.

To access an element in the list by its index number, first write the name of the list, then in square brackets write the integer of the element's index.

For example, if you wanted to access the element that has an index of 2, you'd do:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"] print(names[2]) #output #Kenny

Python List append()

In this tutorial, we will learn about the Python list append() method with the help of examples.

The append() method adds an item to the end of the list.

Example

currencies = ['Dollar', 'Euro', 'Pound']
# append 'Yen' to the list currencies.append('Yen')
print(currencies)
# Output: ['Dollar', 'Euro', 'Pound', 'Yen']