How to Append String to Beginning Of List Python

This Python tutorial will show us various approaches to adding a string to the beginning of a list in Python. Because the list insertion technique must be the same irrespective of what type of data the list includes, a list of strings is going to be used examples throughout the course to concentrate on list insertion rather than inserting other data types.

  • Append String to Beginning of List Python

Append String To Beginning of List Python

The new element is often added at the end of a Python list using the append function. However, there are some circumstances where we must append each entry we add to the beginning of the list. Let’s talk about some methods for doing an append at the start of the list.

To append a string to the beginning of a list in Python, use the insert() function

The insert() function adds a new item to an existing list at the specified index. It accepts two parameters: the value to insert and the index into which the item should be entered.

insert(indx, value)

As an illustration, we’ll add a string to a three-item existing list. By setting the first argument to 0, which indicates that the insertion is made at index 0 (the list’s start), we use insert() function to append a string at the beginning of the list.

Create or initialize the new list with three strings that represent the three cities of the USA using the below code.

usa_citi = ["New York","Los Angeles", "Chicago"]
print(usa_citi)

Use the function insert() to insert the new string “Dallas” at the beginning of the above-created list.

usa_citi.insert(0,"Dallas")

Print the values of the list “usa_citi” using the below code.

print(usa_citi)
Append String To Beginning of List Python
Append String To Beginning of List Python

In the above output, we can see that the string “Dallas” is appended at the beginning of the list.

Read How to get string values from list in Python

To append a string to the beginning of a list in Python, use the + and [] Operator

The string appending at the beginning task can be completed by combining these two operators in Python. The element is transformed into a list, and the list addition is then carried out.

Create or initialize the new list with three strings that represent the three cities of the USA using the below code.

usa_citi = ["New York","Los Angeles", "Chicago"]
print(usa_citi)

Use the operators + and [] to append the string “Dallas” to the beginning of the above-created list “usa_citi” using the below code.

usa_citi = ["Dallas"] + usa_citi

Keep in mind that the string “Dallas” is enclosed in square brackets []. In order to enable list addition, the single string is transformed into the list data type.

View the list using the below code.

print(usa_citi)
Append String To Beginning of List Python Example
Append String To Beginning of List Python Example

In the above output, we can see that the string “Dallas” is appended at the beginning of the list using the two operators + and [].

This is how to append a string to the beginning of a list in Python, using the + and [] Operator.

To append a string to the beginning of a list in Python, use the slicing method

List slicing is yet another way to carry out this specific operation. We simply attach the list created from the transformed element to the 0-sliced list in Python.

Create or initialize the new list with three strings that represent the three cities of the USA using the below code.

usa_citi = ["New York","Los Angeles", "Chicago"]
print(usa_citi)

Use the slicing to append the string at the beginning of the above-created list using the below code.

usa_citi[:0] = ["Dallas"]

Check the appended string at the beginning of the list using the below code.

print(usa_citi)
Append String To Beginning of List Python Using Slicing
Append String To Beginning of List Python Using Slicing

This is how to append a string to the beginning of a list in Python, using the slicing method.

Read How to get unique values from a list in Python

Append an element at the start of a Python list using unpacking

In Python, a procedure called unpacking makes certain iterable manipulations possible. The iterable assignment is more adaptable and effective for developers because of unpacking.

In this example, inserting at the start of the list will be accomplished by merging already-existing iterables, which is a feature of unpacking. We combine a single string with the current list using the unpacking operator *, inserting the string at the start of the newly created list.

Create or initialize the new list with three strings that represent the three cities of the USA using the below code.

usa_citi = ["New York","Los Angeles", "Chicago"]
print(usa_citi)

Use the unpacking method to append the string at the beginning of the above-created list using the below code.

new_str = "Seattle"

usa_citi = [new_str,*usa_citi]

Show the newly appended string to the list using the below code.

print(usa_citi)
Append String To Beginning of List Python Using Unpacking
Append String To Beginning of List Python Using Unpacking

This is how to append an element at the start of a list using unpacking in Python.

Read Python find index of element in list

To append a string to the beginning of a list in Python, use the collections.deque.appendleft() method

The list can be transformed into a deque, and then the push-like action can be carried out at the beginning of the doubly-ended queue using appendleft().

Import the required libraries or methods using the below code.

from collections import deque

Create or initialize the new list with three strings that represent the three cities of the USA using the below code.

usa_citi = ["New York","Los Angeles", "Chicago"]
print(usa_citi)

First deque the above-created list using the below code.

usa_citi = deque(usa_citi)

Append the new string “Dallas” at the beginning of the list using the below code.

usa_citi.appendleft("Dallas")

Again convert it into the list using the below code.

usa_citi = list(usa_citi)

View the list using the below code.

print(usa_citi)
Append String To Beginning of List Python Using Deque
Append String To Beginning of List Python Using Deque

This is how to append a string to the beginning of a list in Python, using the collections.deque.appendleft() method.

We have learned about how to append the string at the beginning of the List in Python using different approaches or methods such as using the insert(), plus(+), and square brackets([]) operator, slicing, unpacking, and deque() method.

You may like the following Python tutorials: