Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Ways to print list without quotes

Python | Ways to print list without quotes

Whenever we print list in Python, we generally use str(list) because of which we have single quotes in the output list. Suppose if the problem requires to print solution without quotes. Let’s see some ways to print list without quotes. 

Method #1: Using map() 

Python3




# Python code to demonstrate
# printing list in a proper way
 
# Initialising list
ini_list = ['a', 'b', 'c', 'd']
 
# Printing initial list with str
print ("List with str", str(ini_list))
 
# Printing list using map
print ("List in proper method", '[%s]' % ', '.join(map(str, ini_list)))


Output:

List with str ['a', 'b', 'c', 'd']
List in proper method [a, b, c, d]

Time Complexity: O(n) where n is the number of elements in the list “ini_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new list 

  Method #2: Using sep Method 

Python3




# Python code to demonstrate
# printing list in proper way
 
# Initialising list
ini_list = ['a', 'b', 'c', 'd']
 
# Printing initial list with str
print ("List with str", str(ini_list))
 
# Printing list using sep Method
print (*ini_list, sep =', ')


Output:

List with str ['a', 'b', 'c', 'd']
a, b, c, d

Time Complexity: O(n), where n is the length of the list ini_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the ini_list 

  Method #3: Using .format() 

Python3




# Python code to demonstrate
# printing list in proper way
 
# Initialising list
ini_list = ['a', 'b', 'c', 'd']
 
# Printing initial list with str
print ("List with str", str(ini_list))
 
# Printing list using .format()
print ("Printing list without quotes", ("[{0}]".format(
                       ', '.join(map(str, ini_list)))))


Output:

List with str ['a', 'b', 'c', 'd']
Printing list without quotes [a, b, c, d]

Time Complexity: O(n) where n is the number of elements in the list “test_list”. Using .format() operator performs n number of operations.
Auxiliary Space: O(n), where n is the length of list

  Method #4: Using translate Method 

Python3




# Python code to demonstrate
# printing list in proper way
 
# Initialising list
ini_list = ['a', 'b', 'c', 'd']
 
# Printing initial list with str
print ("List with str", str(ini_list))
 
translation = {39: None}
# Printing list using translate Method
print ("Printing list without quotes",
        str(ini_list).translate(translation))


Output:

List with str ['a', 'b', 'c', 'd']
Printing list without quotes [a, b, c, d]

Method #6: Using f-strings

F-strings are a way to embed expressions inside string literals, using {}. The above code uses the join method to join the elements of the list with a comma, and then embeds the resulting string inside [] using an f-string.

Python3




# Python code to demonstrate
# printing list in proper way
ini_list = ['a', 'b', 'c', 'd']
# Printing initial list with str
print ("List with str", str(ini_list))
# Printing list using f string
print(f"[{', '.join(ini_list)}]")
#This code is contributed by Edula Vinay Kumar Reddy


Output

List with str ['a', 'b', 'c', 'd']
[a, b, c, d]

Time complexity: O(n) 

Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments