Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

he66666 Group Title

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...

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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?

    • 2 years ago
  2. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • 2 years ago
  3. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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']

    • 2 years ago
  4. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Okay, try that code I posted. Should do the trick.

    • 2 years ago
  5. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I tried testing it but it keeps saying "NameError: name 'concat_list' is not defined". I don't know why there's an error....

    • 2 years ago
  6. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Hmm... and you copied and pasted it?

    • 2 years ago
  7. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    yes, and I rearranged it and indented in the way it was posted here. There shouldn't be a reason an error would occur...

    • 2 years ago
  8. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Can you post here what you have now?

    • 2 years ago
  9. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • 2 years ago
  10. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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']])

    • 2 years ago
  11. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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..

    • 2 years ago
  12. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • 2 years ago
  13. he66666 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    Oh I see. thanks again :)

    • 2 years ago
  14. keketsu Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    No problem. Glad to help!

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.