Append String to a List in Python

If you want to append a string to a list, you can do it using the append function.

my_list = [1, 2, 3]
my_str = 'test'

my_list.append(my_str)
print("The new list is : " + str(my_list))

The string is added to the end of the list. Now the list consists of 3 numbers and one string.

The new list is : [1, 2, 3, 'test']

The Plus (+) Operator

You can achieve a similar effect using the plus operator.

my_list = [1, 2, 3]
my_str = 'test'

my_list += [my_str]
print("The new list is : " + str(my_list))

If you run the code the result is identical.

The new list is : [1, 2, 3, 'test']

my_list += [my_str] is the same as my_list = my_list + [my_str]. It’s just a shorter notation used in many programming languages, including Python.

Notice that the string is added as the whole list element, that’s why it’s in the square brackets: [my_str].

Append Individual Elements of the String

In the last part of the tutorial, we used brackets to add the string as a single element.

Let’s take a look at what happens if we remove these brackets.

my_list = [1, 2, 3]
my_str = 'test'

my_list += my_str
print("The new list is : " + str(my_list))

Now, each character of the string is added as a single element.

The new list is : [1, 2, 3, 't', 'e', 's', 't']

Append String to Empty List

If you want to make a list consisting only of the string characters, you can do it by creating an empty list and adding each character separately.

my_list = []
my_str = 'test'

my_list += my_str
print("The new list is : " + str(my_list))

Now, there are only string characters inside the list.

The new list is : ['t', 'e', 's', 't']

Use Loop

You can use a loop to append individual characters. For this, we are going to use the for loop and the append function.

my_list = []
my_str = 'test'

for character in my_str:
    my_list.append(character)
    print(my_list)

print("The new list is : " + str(my_list))

The for loop goes through each character of the string and adds it to the end of the list.

Inside the loop, the actual value of my_list is printed each time the character is added to the end of the list and the final result is the same as before.

['t']
['t', 'e']
['t', 'e', 's']
['t', 'e', 's', 't']
The new list is : ['t', 'e', 's', 't']

Append String in a Loop in Python

If you have a list of strings and you want to concatenate them to create a single string made of these elements, you can use the For loop.

list_of_strings = ['one', 'two', 'three']
my_string = ''

for word in list_of_strings:
    my_string += str(word)

print("Final result:", my_string)

With each pass of the loop, the next word is added to the end of the string. The result is a single word.

Final result: onetwothree

The problem with this approach is that there are no separators between characters. We can easily fix that inside the loop. We are going to separate words with a comma.

list_of_strings = ['one', 'two', 'three']
my_string = ''

for word in list_of_strings:
    my_string += str(word + ",")

my_string = my_string[:-1]
my_string += '.'
print("Final result:", my_string)

Now, with each pass, there is a word and comma added to the end of the string.

Before printing the result, we have to remove the comma at the end of the string, that was added in the last pass.

my_string = my_string[:-1]

This code assigns the my_string variable without the last character (which is a comma) to itself.

At the end of the string, we are going to add a dot.

my_string += '.'

If you run this code, you are going to get this result.

Final result: one,two,three.

The While Loop

If you prefer to use the While loop, you need to create a counting variable and know how many words are there inside the list.

list_of_strings = ['one', 'two', 'three']
my_string = ''

counter = 0

while counter < list_of_strings.__len__():
    my_string += str(list_of_strings[counter] + ",")
    counter += 1

my_string = my_string[:-1]
my_string += '.'
print("Final result:", my_string)

This loop continues to meet the requirement when the counter variable is lower than the number of words inside the list. Each time the counter variable is incremented.

counter += 1

This code returns the same result as before:

Final result: one,two,three.

Append One String to Another in Python

The simplest way to append one string to another is by using the addition operator. The same as you use to add two numbers.

pre_text = "This is"
post_text = " text."
text = pre_text + post_text

print(text)

This code concatenates two strings and displays the result to the console.

This is text

Notice, that the post_text variable has a space before the text. It’s important for separating words.

If you have a list of words that you want to join, but there are no spaces between them, you can add words to a list and merge them with the join function.

pre_text = "This"
mid_text = "is"
post_text = "text."
text = " ".join([pre_text, mid_text, post_text])

print(text)

Space before join is a separator to separate the words. This is the result.

This is text.

Append Words using the For Loop

A string in Python is a list of characters. You can loop through each element of a string and add them to the end of another string.

pre_text = "This is"
post_text = " text."

for char in post_text:
    pre_text += char
    print(pre_text)

print("Final result:", pre_text)

With each step inside the loop, the variable is printed out to the console.

This is 
This is t
This is te
This is tex
This is text
This is text.
Final result: This is text.

How to Convert Unicode to String in Python

You can convert Unicode characters to ASCII string using the encode function.

mytext = "Klüft électoral große"
myresult = mytext.encode('ascii', 'ignore')

print(myresult)

All values that are not ASCII characters will be ignored.

b'Klft lectoral groe'

In the encode function, there is a second parameter. In this case, it’s ignoring characters that don’t meet the requirement.

There are also different parameters, for example, replace. In this case, Python inputs question marks, instead of removing the characters, so the result consists of the same amount of characters as the entry string.

The new code looks like this:

mytext = "Klüft électoral große"
myresult = mytext.encode('ascii', 'replace')

print(myresult)

And this is the result.

b'Kl?ft ?lectoral gro?e'

Normalization forms

There is also an option to convert characters to the closest equivalent from ASCII.

For this purpose, we are going to use the normalize function. There are also a few parameters, you can use, but for this demonstration, I’m going to use only one: NFKD.

This is how the code looks like:

import unicodedata

mytext = "Klüft électoral große"
myresult = unicodedata.normalize('NFKD', mytext).encode('ascii', 'ignore')
print(myresult)

Here’s the result:

b'Kluft electoral groe'

Convert ß to ss

In this case, the sharp S (ß) was not converted to “ss”, but rather ignored. We can quickly fix that by adding the replace function to mytext variable. It has to be replaced before the normalize function.

mytext = "Klüft électoral große".replace('ß', 'ss')

Now, when you run the code the sharp S is not lost.

b'Kluft electoral grosse'

ASCII and UTF-8

Instead of ASCII, you can also use the UTF-8 encoding.

mytext = "Klüft électoral große"
myresult = mytext.encode('utf-8')
print(myresult)

This is how the result looks like:

b'Kl\xc3\xbcft \xc3\xa9lectoral gro\xc3\x9fe'

Sort List by Key in Python

When we sort a list it’s sort by values. If we have a list of tuples, it’s sort by the first element by default.

my_list = [(5, 7), (4, 3), (8, 6), (3, 5)]
my_list.sort()

print('Sorted list:', my_list)

This code will sort by the fist element of a tuple.

Sorted list: [(3, 5), (4, 3), (5, 7), (8, 6)]

Let’s that we want to use a key to sort a list by the second element.

# takes second element for sort
def secondElement(elem):
    return elem[1]

my_list = [(5, 7), (4, 3), (8, 6), (3, 5)]

# sorts with a key
my_list.sort(key=secondElement)

print('Sorted list:', my_list)

Now, as you can see the second element is used to sort a list. All values are sorted in the ascending order: 3, 5, 6, 7.

In a similar way, you can sort a list by the third element:

# takes second element for sort
def thirdElement(elem):
    return elem[2]

my_list = [(5, 7, 4), (4, 3, 8), (8, 6, 2), (3, 5, 1)]

# sorts with a key
my_list.sort(key=thirdElement)

print('Sorted list:', my_list)

This time the list is sorted by the first element of the tuple elem[2].

Sorted list: [(3, 5, 1), (8, 6, 2), (5, 7, 4), (4, 3, 8)]

Install BeautifulSoup Using PIP

To install BeautifulSoup, follow these steps:

  1. Open Command Line as administrator
  1. Enter the command:
pip install beautifulsoup4

Now, the BeautifulSoup library is installed and you can check what is the current version.

But first, you need to open the Python interpreter. Enter “python” in the command line and press Enter:

Next, insert these lines:

import bs4
print(bs4.__version__)

In my case, the version is 4.9.3

Test BeautifulSoup

Now,  you can easily test this library and check whether it works. This code scrapes a site and returns the title of this site.

from bs4 import BeautifulSoup
import requests

url = 'https://example.com/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.find('title')
print(title.text)

And this is the result:

Example Domain

Clear File in Python

The quickest way to clear file contents in Python is by using the following code:

file = open("D:/my_file.txt","w")
file.close()

This code will open the file for writing and close it in the next line.

If there is no information entered, the file will erase contents and become empty, so there is no need for clearing the file before writing.

If there is no file, Python will create an empty text file.

This is the file we are going to use in the next examples:

Clear file if exists

If you don’t want Python to create a file if it doesn’t exist, you have to check whether the file is in the specified location before writing to a file.

try:
    test = open("D:/my_file.txt", "r")
    test.close()
    file = open("D:/my_file.txt", "w")
    file.close()
except IOError:
    print("There is no such file")

First, Python tries to open the file for reading. If it fails, it automatically returns an exception under except. If there is a file, it will be opened for reading and after that for writing.

The file is closed before anything is written to it, so all data inside the file is erased.

Clear file after reading

Now, let’s try to read the file and clear it after that.

file = open("D:/my_file.txt", "r")
lines = file.read().splitlines()
file.close()
file = open("D:/my_file.txt", "w")
file.close()

print(lines)

Each line inside the file is saved as a list element before clearing file contents.

The printed result is:

['First line', 'Second line', 'Third line']

Clear file line by line

You can also clear a file line by line:

file = open("D:/my_file.txt", "r")
lines = file.readlines()
file.close()

file = open("D:/my_file.txt", "w")
for line in lines:
    file.write("")

file.close()

First, this code reads lines from the file to a list. Next, for each of these lines, it writes an empty string, therefore clearing these lines.

Erase the last line

To remove the last line from the file, first, we have to count the number of lines in a file. Then we have to write an empty string to the last one.

Here’s the code:

file = open("D:/my_file.txt", "r")
lines = file.readlines()
last_line = len(lines) - 1
file.close()

file = open("D:/my_file.txt", "w")
for i, line in enumerate(lines):
    if i != last_line:
        file.write(line)
    else:
        file.write("")

file.close()

The following line returns the number of text lines in a file:

last_line = len(lines) - 1

We have to subtract one line because the indexing in the loop starts from 0 and not from 1.

The enumerate function returns the index of the current loop and a value. If the index is the same as the last_line – 1 then it writes “” instead of the current line.

Remove selected line

In a similar way, you can give a number of the line you want to remove. Let’s say you want to remove the second line from the file.

file = open("D:/my_file.txt", "r")
lines = file.readlines()
line_to_remove = 2
file.close()

file = open("D:/my_file.txt", "w")
for i, line in enumerate(lines):
    if i != line_to_remove - 1:
        file.write(line)
    else:
        file.write("")

file.close()

Remove selected lines

If you want to delete more than a single line, you can use a list of lines to remove. Let’s modify our file a bit by adding additional lines:

Run the code to remove multiple lines. In our case they will be 2, 5, and 7.

file = open("D:/my_file.txt", "r")
lines = file.readlines()
lines_remove = [2, 5, 7]
file.close()

file = open("D:/my_file.txt", "w")
for i, line in enumerate(lines):
    if i + 1 in lines_remove:
        file.write("")
    else:
        file.write(line)

file.close()

First, there are three numbers added to a list. These are the numbers of lines we want to remove.

lines_remove = [2, 5, 7]

Later in the code, we check if the current line is inside the list. If the condition is met, the line is removed from the file.

If you open the file, you will notice that selected lines are removed.

Clear lines with the provided string

You can remove lines that are the same as provided text.

file = open("D:/my_file.txt", "r")
lines = file.readlines()
file.close()

file = open("D:/my_file.txt", "w")
for line in lines:
    if line.strip("\n") != "Second line":
        file.write(line)

file.close()

The file content is saved as list elements.

In the next part, Python checks for each line whether the string equals the “Send line”. If it’s not then it writes it to a file, otherwise, it drops it.

The newline characters are stripped from the file for each line.

This is how our new file looks like:

You can also use substrings, insead of whole strings.

Just replace this line:

if line.strip("\n") != "Second line":

With this one:

if "Second" not in line.strip("\n"):

This code will remove all lines that contain the word “Second”. It’s case-sensitive.

The result is the same as in the previous example.

Append a File to a List in Python

Last time I wrote an article on how to write a list to a file. This time we are going the other way around: How to read data from a file and write it to a list.

The simplest way to append a file to a list is by using the following code:

with open('my_file.txt', 'r') as f:
    my_names = f.readlines()

Let’s see how it works with the following text file.

Let’s print it:

['First line\n', 'Second line\n', 'Third line']

The first two elements end with the newline character (\n).

Let’s use the for loop:

for name in my_names:
    print(name)

This is how the result looks like:

First line

Second line

Third line

They are separated by the newline character. If you want to get rid of it, you can use the strip function.

with open('C:/my_file.txt', 'r') as f:
    my_names = [line.strip() for line in f]

Now, there are no newline characters at the end of the file.

First line
Second line
Third line

Append CSV file to a list

If you are using a CSV file, instead of the text file, the code will be slightly more complicated.

This is the CSV file opened in a text editor:

If you try to use the previous code, you are going to get the following result:

['John,Smith,age,24', 'Alex,Miller,age,53', 'Eric,Williams,age,56']

If that’s what’s you want, then ok, but you may want to create a list of lists.

my_people = []

with open('C:/my_file.csv', 'r') as f:
    my_elements = [line.strip() for line in f]
    for element in my_elements:
        elements = element.split(',')
        my_people.append(elements)

for person in my_people:
    print(person)

Now, you have a beautiful list of lists:

['John', 'Smith', 'age', '24']
['Alex', 'Miller', 'age', '53']
['Eric', 'Williams', 'age', '56']