<jupyter_start><jupyter_text>LINQ analogs in Python [Article](http://mark-dot-net.blogspot.com/2014/03/python-equivalents-of-linq-methods.html)<jupyter_code>fruit = ['apple', 'banana', 'citrus', 'dashberry', 'eagleplum', "eelplum"]<jupyter_output><empty_output><jupyter_text>All & Any<jupyter_code>any("e" in f for f in fruit)
all("e" in f for f in fruit)<jupyter_output><empty_output><jupyter_text>Min & Max<jupyter_code>max(len(f) for f in fruit)
min(len(f) for f in fruit)<jupyter_output><empty_output><jupyter_text>Take, Skip, TakeWhile & SkipWhile<jupyter_code>from itertools import islice

list(islice(fruit, 2))       # Take(2)
list(islice(fruit, 2, None)) # Skip(2)
list(islice(fruit, 2, 4))    # Skip(2).Take(2)

from itertools import takewhile
from itertools import dropwhile

list(takewhile(lambda c: len(c) < 7, fruit)) # TakeWhile(f => f.Length < 7)
list(dropwhile(lambda c: len(c) < 7, fruit)) # SkipWhile(f => f.Length < 7)<jupyter_output><empty_output><jupyter_text>First, FirstOrDefault, & Last<jupyter_code>next(f for f in fruit if f.startswith("b")) # First
next((f for f in fruit if f.startswith("q")), "none") # FirstOrDefault
def lastOrDefault(sequence, default=None):
    lastItem = default
    for s in sequence:
        lastItem = s
    return lastItem

lastOrDefault((f for f in fruit if f.endswith("e")))
lastOrDefault((f for f in fruit if f.startswith("x")), "no fruit found")<jupyter_output><empty_output><jupyter_text>Count<jupyter_code>sum(1 for f in fruit if f.startswith("a"))<jupyter_output><empty_output><jupyter_text>Select & Where<jupyter_code>list(map(lambda x: x.upper(), fruit))
list(filter(lambda x: "n" in x, fruit))<jupyter_output><empty_output><jupyter_text>GroupBy Python’s groupby expects the incoming data to be sorted by the key, so you have to call sorted first<jupyter_code>def printGroupedData(groupedData):
    for k, v in groupedData:
        print("Group {} {}".format(k, list(v)))

from itertools import groupby
keyFunc = lambda f: f[0]
sortedFruit = sorted(fruit, key=keyFunc)
printGroupedData(groupby(sortedFruit, keyFunc))<jupyter_output>Group a ['apple']
Group b ['banana']
Group c ['citrus']
Group d ['dashberry']
Group e ['eagleplum', 'eelplum']<jupyter_text>OrderBy<jupyter_code>sorted(fruit, key=lambda x:len(x))<jupyter_output><empty_output><jupyter_text>Distinct<jupyter_code>def distinct(sequence):
    seen = set()
    for s in sequence:
        if not s in seen:
            seen.add(s)
            yield s<jupyter_output><empty_output><jupyter_text>Zip<jupyter_code>recipes = ['pie','juice','milkshake']
list(f + " " + r for f,r in zip(fruit,recipes))<jupyter_output><empty_output><jupyter_text>Dictionaries and Sets<jupyter_code>fruit = ['apples','oranges','bananas','pears']
[len(f) for f in fruit]
{len(f) for f in fruit}
{f:len(f) for f in fruit}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14706914706914706, 'fraction_numerical': 0.00702000702000702, 'mean_word_length': 6.5, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '194348', 'license': 'MIT', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1076, 'n_words': 238, 'path': 'Tutorial/LINQ.ipynb', 'repo_name': 'FallenGameR/LearningPython', 'size': '2849'}