Skip to Content

How to remove quotes in a list in Python?

When working with lists of strings in Python, it is common to encounter situations where you need to print the list without the quotes surrounding each element. This tutorial will guide you through the process of removing quotes from a list in Python. Specifically, we will focus on how to print a list of strings without quotes, using a simple example to illustrate the process.

By the end of this tutorial, you will have a clear understanding of how to remove quotes from a list in Python and be able to apply this knowledge to your own projects.

What do we mean by removing quotes from a list?

Before we proceed with the solution, let’s look at an example of what we are trying to achieve here. Let’s say you have a list of strings, for example, [“Jim”, “Pam”, “Dwight”, “Angela”, “Tobi”]. If you print this list in Python, the list printed has quotes around each element.

# create a list of strings
ls = ["Jim", "Pam", "Dwight", "Angela", "Tobi"]
# print the list
print(ls)

Output:

['Jim', 'Pam', 'Dwight', 'Angela', 'Tobi']

What if you want the printed list to not have quotes surrounding each element? In this tutorial, we will look at some methods with which we can achieve this.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

Methods to remove quotes from a list of strings in Python

There are multiple ways in which you can print a list of strings without the quotes around each list element. Let’s look at them in detail with the help of some examples.

1) Using the string join() function

The idea here is instead of printing a list, we print a string representing the list such that it does not have quotes surrounding each word in the list. Let’s look at an example.

# create a list of strings
ls = ["Jim", "Pam", "Dwight", "Angela", "Tobi"]
# print a string representing the list
print("["+ ", ".join(ls)+ "]")

Output:

[Jim, Pam, Dwight, Angela, Tobi]

Here, we use the string join() function to join together the values in the list such that we get a comma separated string. We then concatenate the resulting string in the middle of the string values “[” and “]” such that the resulting string looks like a list with comma separated values.

Instead of using string concatenation, you can also use a format string.

# create a list of strings
ls = ["Jim", "Pam", "Dwight", "Angela", "Tobi"]
# print a string representing the list
print("[{}]".format(", ".join(ls)))

Output:

[Jim, Pam, Dwight, Angela, Tobi]

We get the same result as above.


2) Using a for loop

This method is similar to the previous method. We are also here printing a string representing the list values without any quotes. The idea here is, we first print the string “[” and then we iterate through the list of strings and print each element with “, ” as the end string, finally after the loop we print “]”.

# create a list of strings
ls = ["Jim", "Pam", "Dwight", "Angela", "Tobi"]
# print a string representing the list
print("[", end="")
for index, item in enumerate(ls):
    if index == len(ls)-1:
        print(item, end="")
    else:
        print(item, end=", ")
print("]")

Output:

[Jim, Pam, Dwight, Angela, Tobi]

The end parameter in the print() function in Python specifies what character(s) should be printed at the end of the output. By default, the end parameter is set to '\n', which means that a newline character is printed at the end of the output. However, you can change this behavior by passing a different string to the end parameter.

We get the same result as above. Note that this method is not as neat as using the join() function.

Conclusion

In conclusion, removing quotes from a list of strings in Python is a simple task that can be achieved using multiple ways such as the join() function or a for loop. The join() function is a more concise and efficient method, especially when dealing with large lists. On the other hand, using a for loop provides more flexibility and control over the output.

It is important to note that removing quotes from a list of strings can be useful in various scenarios, such as when working with data that needs to be formatted in a specific way or when printing output for readability purposes.

You might also be interested in –


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.