he66666
  • he66666
Help with Python- making list functions: def get_str_list(list_strs): """(list of lists of strs) -> list of strs Return a new list of strings, where each string is the concatenation of the strings from the corresponding element of list_strs""" Can someone help me with making this function? I know what it's asking but I don't know where to start...
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
To clarify, list_strs itself is a list of strings? And you're iterating through that list, returning a new list that concatenates each list element from list_strs?
anonymous
  • anonymous
If I'm understanding correctly, here's one way to do it using nested loops. def get_str_list(list_strs): concat_list = [] for outer in list_strs: concat_str = '' for inner in outer: concat_str += inner concat_list.append(concat_str) return concat_list
he66666
  • he66666
yes, list_strs is a new list of strings. so for example if the list was colors = [['black', 'white'] , ['pink', 'red'], the output would be colors = ['blackwhite', 'pinkred']

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Okay, try that code I posted. Should do the trick.
he66666
  • he66666
I tried testing it but it keeps saying "NameError: name 'concat_list' is not defined". I don't know why there's an error....
anonymous
  • anonymous
Hmm... and you copied and pasted it?
he66666
  • he66666
yes, and I rearranged it and indented in the way it was posted here. There shouldn't be a reason an error would occur...
anonymous
  • anonymous
Can you post here what you have now?
he66666
  • he66666
def get_str_list(list_strs): """(list of lists of strs) -> list of strs Return a new list of strings, where each string is the concatenation of the strings from the corresponding element of list_strs""" concat_list = [] for outer in list_strs: concat_str = '' for inner in outer: concat_str += inner concat_list.append(concat_str) return concat_list
anonymous
  • anonymous
huh. I just copied and pasted that and it works. But did you add additional code to feed to that function? I've added an extra line at the end, here. Did you add something else that might be causing the error? def get_str_list(list_strs): """(list of lists of strs) -> list of strs Return a new list of strings, where each string is the concatenation of the strings from the corresponding element of list_strs""" concat_list = [] for outer in list_strs: concat_str = '' for inner in outer: concat_str += inner concat_list.append(concat_str) return concat_list print get_str_list([['a','b','c'],['x','y','z']])
he66666
  • he66666
Oh now it works! something must've gone wrong before. Thanks so much keketsu! but can you explain concat_str = '' and concat_str += inner? I don't really get it..
anonymous
  • anonymous
Sure thing. Oh, I used single quotes there which may have thrown you off. concat_str = "" just declares the variable concat_str and sets it to an empty string. concat_str += inner is the same as concat_str = concat_str + inner So you begin with an empty string, then add inner to it (the first item in the list), and continue to add list items until they're exhausted.
he66666
  • he66666
Oh I see. thanks again :)
anonymous
  • anonymous
No problem. Glad to help!

Looking for something else?

Not the answer you are looking for? Search for more explanations.