8 ways to add an element to the beginning of a list and string in Python
Tags: python, tutorial, programming
Published: 2022-06-03 23:01:19

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:

```python
arr = [1, 2, 3]
arr.insert(0, 5)
print(arr)
```

Result:

```python       
[5, 1, 2, 3]
```

### 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:

```python
arr = [1, 2, 3]
print([5] + arr)
```

The result will be the same as in the first method:
      
```python
[5, 1, 2, 3]
```

### 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:

        
```python
arr = [1, 2, 3]
num = [5]
num.append(arr)
print(arr)
```

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

        
```python
[5, [1, 2, 3]]
```

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:

```python      
arr = [1, 2, 3]
num = [5]
num.extend(arr)
print(num)
```

The result is a regular, non-nested list:

```python       
[5, 1, 2, 3]
```

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:

```python     
arr = [1, 2, 3]
num = [5, 6, 7]
num.extend(arr)
print(num)
```

Result:

```python      
[5, 6, 7, 1, 2, 3]
```

## 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:

```python        
el = '+'
num = '123456'
print(el + num)
```

The operation results in a new line:

```python       
+123456
```

### Method 2: Using the f-string

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

```python        
el = '+'
num = '1337'
print(f'{el}{num}')
```

The result will be similar to the first method:

```python        
+1337
```

### 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.:

```python        
el = '+'
num = list('9876')
(num).insert(0, el)
print(''.join(num))
```

The result will be the same:

```python        
+9876
```

### 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 `+`:

```python        
num = "5678"
print(num.rjust(12, '+'))
```

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

```python        
+5678
```

## 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.