GithubHelp home page GithubHelp logo

dsc-enterprise-bain-list-comprehensions-lab's Introduction

List Comprehension

Following exercises require to put the skills you've learned in the previous lesson to practice. All exercises simply require you to create a list using list comprehension to address the question. Let's get started.

Exercise 1:

Create a list of all of the numbers from 1-100 that are divisible by 7

# [0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]

Exercise 2:

Create a list all of the numbers from 1-100 that have a 9 in them

# [9, 19, 29, 39, 49, 59, 69, 79, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[9, 19, 29, 39, 49, 59, 69, 79, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Exercise 3:

Remove all the wovels and spaces from the string "A quick brown fox jumped over the lazy dog". Output a list with only letters from the string - minus the vowels and spaces, as shown the comments.

# Use this string in your list comprehension
string = "A quick brown fox jumped over the lazy dog"
# ['q',
#  'c',
#  'k',
#  'b',
#  'r',
#  'w',
#  'n',
#  'f',
#  'x',
#  'j',
#  'm',
#  'p',
#  'd',
#  'v',
#  'r',
#  't',
#  'h',
#  'l',
#  'z',
#  'y',
#  'd',
#  'g']
['q',
 'c',
 'k',
 'b',
 'r',
 'w',
 'n',
 'f',
 'x',
 'j',
 'm',
 'p',
 'd',
 'v',
 'r',
 't',
 'h',
 'l',
 'z',
 'y',
 'd',
 'g']

Exercise 4:

Find all of the words in a string that have less than 4 letters. Use the string "A quick brown fox jumped over the lazy dog" as before.

string = "A quick brown fox jumped over the lazy dog"
# ['A', 'fox', 'the', 'dog']
['A', 'fox', 'the', 'dog']

Exercise 5:

Use a nested list comprehension to find all of the numbers from 1-25 that are divisible by any single digit besides 1 (2-9)

# [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25]

Conclusion

So now you have a good understanding of how to write list comprehensions in different situations. IN certain situations, such list comprehensions can be used to replace (nested) for loops or lambda functions with map(), filter() and reduce(). You are encouraged to implement these in your coding practice as these would produce a neat looking code that is more efficient and easy to troubleshoot.

dsc-enterprise-bain-list-comprehensions-lab's People

Contributors

shakeelraja avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.