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

susanjrobertson Group Title

I'm having a hard time with the final written exercise. I've read about list comprehensions, but something isn't clicking for me. Any help is appreciated.

  • 2 years ago
  • 2 years ago

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

    they are just another way of producing a new list. if a = [1,2,3,4,5,6,7,8], we could produce a list of the squared elements of a: sqrs = [] # an empty list for x in a: # for each element of a b.append(x*x) # add x*x to the list b list comprehensions just give you a more concise mechanism to do it: b = [ x*x for x in a] # b is the same list as produced above. if we have some function def def f(x): return x*x*x + 17 we can produce a list of f(i) for each i in a b = [f(i) for i in a] which is the list [f(1),f(2)...f(8)] we can even add a condition: b = [x for x in a if x % 2 == 0] this is the same as: b = [] for x in a: if x % 2 == 0: b.append(x) a list comprehension is only a shorter (different) way of doing it, despite the name, nothing really new. play with a few in idle. a = [1,2,3,4], b = [x/2 for x in a], and print b. I hope I haven't made things worse...

    • 2 years ago
  2. andrew.m.higgs Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Very nice concise answer. Thank you for asking the question Susan. That gave snark the opportunity to answer.

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

    Thanks so much - I think I'm getting it :)

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

    Thank you for bringing it up. I was struggling as well and was unfamiliar with list comprehensions. I'll study up.

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