Python Forum
list of strings to a single string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of strings to a single string
#1
How do I convert a list of strings into a single string in a function where the list of strings is my argument? Without using join?
If you have a dictionary with names and some sentences.

This is what I had in mind? 
def somefunction(list_strings):
   #list_strings = dictionary.values()
    for value in list_strings:
        string = list_strings.append()
    return string
Reply
#2
Create an empty string before the loop.
Add value to the string on each loop.
Return the string after the loop.
Reply
#3
(Nov-01-2016, 05:34 PM)Yoriz Wrote: Create an empty string before the loop.
Add value to the string on each loop.
Return the string after the loop.

Thanks! But how do I import a dictionary from another function into this function? 
And is the empty string just created by this 

string = ''

And is it correct to use append(value) into the empty string?
Reply
#4
string_list = ['s', 't', 'r', 'i', 'n', 'g', 's']
string = ''
for item in string_list:
    string += s
print string
>> strings
Use +=, they must be the same type or you will get a TypeError.
Reply
#5
(Nov-04-2016, 02:01 AM)kopuz Wrote:
string_list = ['s', 't', 'r', 'i', 'n', 'g', 's']
string = ''
for item in string_list:
    string += s
print string
>> strings
Use +=, they must be the same type or you will get a TypeError.

This will throw an error. The 's' variable is not defined.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Nov-04-2016, 07:20 AM)wavic Wrote: This will throw an error. The 's' variable is not defined.
And regardless, str.join is the recommended method.
>>> string_list = ['s', 't', 'r', 'i', 'n', 'g', 's']
>>> "".join(string_list)
'strings'
>>>
Reply
#7
But the task is to do it without using join()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Nov-04-2016, 07:41 AM)wavic Wrote: But the task is to do it without using join()

is that to make it a coding challenge or do you have a real-life need where str.join() cannot be used
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Nov-01-2016, 04:58 PM)tebirkes Wrote: How do I convert a list of strings into a single string in a function where the list of strings is my argument? Without using join?

I did not request it
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Yeah I missed that in the original post, but the other way is actually bad for more than idiomatic reasons.  Depending on your OS, string concatenation is not guaranteed to be optimized.  It tends to be optimized in Linux versions and not in Windows versions in my experience, but the point is that behavior is not guaranteed in spec.  This means you can write an alg on linux that runs in O(n) and then suddenly encounter users complaining about performance  because they are getting O(n^2).  This is very hard to debug unless you are aware of it.

I hope that this has been addressed in all versions in the current 3x version but I am not aware if it has.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 467 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 690 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 2,012 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Help with Logical error processing List of strings dmc8300 3 1,080 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  python sql query single quote in a string mg24 1 1,050 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,754 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 7,995 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,343 Apr-11-2021, 11:03 PM
Last Post: lastyle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020