I am using the latest Python 3
letters = ['a', 'b', 'c', 'd', 'e']
letters[:3]
print((letters)[:3])
letters[3:]
print((letters)[3:])
print("Here is the whole thing :" + letters)
Error:
Traceback (most recent call last):
File "C:/Users/Computer/Desktop/Testing.py", line 6, in <module>
print("Here is the whole thing :" + letters)
TypeError: Can't convert 'list' object to str implicitly
When fixing, please explain how it works :) i dont want to just copy a fixed line
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
All type can be converted in String in python using str.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList); Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object .
A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values. Set<String> set = new HashSet<>(list);
As it currently stands, you are trying to concatenate a string with a list in your final print statement, which will throw TypeError
.
Instead, alter your last print statement to one of the following:
print("Here is the whole thing :" + ' '.join(letters)) #create a string from elements
print("Here is the whole thing :" + str(letters)) #cast list to string
print("Here is the whole thing : " + str(letters))
You have to cast your List
-object to String
first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With