DEV Community 👩‍đŸ’ģ👨‍đŸ’ģ

Cover image for 8 ways to add an element to the beginning of a list and string in Python
Matvey Romanov
Matvey Romanov

Posted on

8 ways to add an element to the beginning of a list and string in Python

Let's look at different methods for adding elements to the beginning of a list and string: concatenation, insert, append, extend, rjust, and f-strings.

How to add an item to the top of a Python list: 4 methods

Lists in Python are mutable and ordered data structures designed to store a group of values. If you need to insert an element in the first position of an existing list during the program execution, you can use one of the methods described below.

Method 1: insert

This method is implemented using the built-in insert() method of lists. This method works with any type of data and allows you to insert the desired element at any position in the existing list, including the first one.

The insert() method takes two parameters – the index (position) at which the element is to be inserted, and the element itself. Counting positions in Python starts from zero – Accordingly, to insert an element at the beginning of the list , you need to specify 0, and not, as the first parameter 1.

Let's take an example. Suppose we have a list [1, 2, 3] where we want to insert the number 5 at the beginning. Use the insert() method:

arr = [1, 2, 3]
arr.insert(0, 5)
print(arr)
Enter fullscreen mode Exit fullscreen mode

Result:

[5, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Method 2: Concatenation

Using concatenation (addition), you can also insert the desired element at the beginning of the list. However, this requires that the element is presented as a list:

arr = [1, 2, 3]
print([5] + arr)
Enter fullscreen mode Exit fullscreen mode

The result will be the same as in the first method:

[5, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Method 3: The append method

By default, this method expands an existing list by adding an element to the end. But nothing prevents you from expanding the list consisting of a single element:

arr = [1, 2, 3]
num = [5]
num.append(arr)
print(arr)
Enter fullscreen mode Exit fullscreen mode

The result when using append() is different – you will get a nested list:

[5, [1, 2, 3]]
Enter fullscreen mode Exit fullscreen mode

The desired element, however, gets an index 0, so the goal is completed. In addition, when solving some tasks, you may need to create a nested list, so this method has the right to live.

Method 4: extend method

The extend() method is similar to append () – with the difference that it preserves the" one-dimensionality " of the list:

arr = [1, 2, 3]
num = [5]
num.extend(arr)
print(num)
Enter fullscreen mode Exit fullscreen mode

The result is a regular, non-nested list:

[5, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Note that one-dimensionality is preserved even if the element to be placed at the beginning of the list is itself a list consisting of several elements:

arr = [1, 2, 3]
num = [5, 6, 7]
num.extend(arr)
print(num)
Enter fullscreen mode Exit fullscreen mode

Result:

[5, 6, 7, 1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

4 ways to add an element to the beginning of a string in Python

Strings in Python are an immutable data type str, and represent sequences of different characters. Since the lines do not change, you can only add elements to the beginning of the sequence by creating a new line.

Method 1: Concatenation

Strings in Python can be concatenated (the result will be a new line). Let's look at the example of inserting a sign +at the beginning of a string containing an abstract mobile number:

el = '+'
num = '123456'
print(el + num)
Enter fullscreen mode Exit fullscreen mode

The operation results in a new line:

+123456
Enter fullscreen mode Exit fullscreen mode

Method 2: Using the f-string

You can also use the f-string to insert an element at the beginning of a line:

el = '+'
num = '1337'
print(f'{el}{num}')
Enter fullscreen mode Exit fullscreen mode

The result will be similar to the first method:

+1337
Enter fullscreen mode Exit fullscreen mode

Method 3: Converting to a list

If you need to insert an element at a specific position in the string, including at the beginning, you can use the join() method to convert the string to a list and merge the list into a string.:

el = '+'
num = list('9876')
(num).insert(0, el)
print(''.join(num))
Enter fullscreen mode Exit fullscreen mode

The result will be the same:

+9876
Enter fullscreen mode Exit fullscreen mode

Method 4: Using rjust

The rjust() method is used to align the line to the right edge. As parameters, it accepts the length of a new line and the character that will be used to fill in empty positions. By default, a space is used as a placeholder, but nothing prevents you from selecting a sign +:

num = "5678"
print(num.rjust(12, '+'))
Enter fullscreen mode Exit fullscreen mode

The result is the desired string with the required character inserted at the beginning:

+5678
Enter fullscreen mode Exit fullscreen mode

Let's sum up the results

We've covered eight simple and practical ways to add the desired element to the beginning of a Python list and string. Do you know any other interesting ways to insert elements? Share it with us in the comments.

Top comments (0)

🤔 Did you know?

 
📚 You can adjust your experience level in Settings to see more relevant content.