# Tutorial by Examples

## Getting the minimum or maximum of several values

min(7,2,1,5) # Output: 1 max(7,2,1,5) # Output: 7

## Using the key argument

Finding the minimum/maximum of a sequence of sequences is possible: list_of_tuples = [(0, 10), (1, 15), (2, 8)] min(list_of_tuples) # Output: (0, 10) but if you want to sort by a specific element in each sequence use the key-argument: min(list_of_tuples, key=lambda x: x) # Sorting ...

## Default Argument to max, min

You can't pass an empty sequence into max or min: min([]) ValueError: min() arg is an empty sequence However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception: max([], default=42) ...

## Special case: dictionaries

Getting the minimum or maximum or using sorted depends on iterations over the object. In the case of dict, the iteration is only over the keys: adict = {'a': 3, 'b': 5, 'c': 1} min(adict) # Output: 'a' max(adict) # Output: 'c' sorted(adict) # Output: ['a', 'b', 'c'] To keep the dictionary ...

## Getting a sorted sequence

Using one sequence: sorted((7, 2, 1, 5)) # tuple # Output: [1, 2, 5, 7] sorted(['c', 'A', 'b']) # list # Output: ['A', 'b', 'c'] sorted({11, 8, 1}) # set # Output: [1, 8, 11] sorted({'11': 5, '3': 2, '10': 15}) # dict # Output: ['10', '11...

## Minimum and Maximum of a sequence

Getting the minimum of a sequence (iterable) is equivalent of accessing the first element of a sorted sequence: min([2, 7, 5]) # Output: 2 sorted([2, 7, 5]) # Output: 2 The maximum is a bit more complicated, because sorted keeps order and max returns the first encountered value. In case th...

## Make custom classes orderable

min, max, and sorted all need the objects to be orderable. To be properly orderable, the class needs to define all of the 6 methods __lt__, __gt__, __ge__, __le__, __ne__ and __eq__: class IntegerContainer(object): def __init__(self, value): self.value = value def __rep...

## Extracting N largest or N smallest items from an iterable

To find some number (more than one) of largest or smallest values of an iterable, you can use the nlargest and nsmallest of the heapq module: import heapq # get 5 largest items from the range heapq.nlargest(5, range(10)) # Output: [9, 8, 7, 6, 5] heapq.nsmallest(5, range(10)) # Output: [...

Page 1 of 1
{"ft_lang_label":"__label__en","ft_lang_prob":0.68900263,"math_prob":0.62009895,"size":1733,"snap":"2021-43-2021-49","text_gpt3_token_len":487,"char_repetition_ratio":0.12608445,"word_repetition_ratio":0.0,"special_character_ratio":0.31679168,"punctuation_ratio":0.21842106,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9596122,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-24T21:04:28Z\",\"WARC-Record-ID\":\"<urn:uuid:5b6f48aa-5b33-4ed8-bd9d-f160f1217828>\",\"Content-Length\":\"23276\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:914d6683-6cd0-4f64-b947-1e8427a7aa6b>\",\"WARC-Concurrent-To\":\"<urn:uuid:f1bc8f9d-bcea-4388-a136-defde795dae2>\",\"WARC-IP-Address\":\"40.83.160.29\",\"WARC-Target-URI\":\"https://riptutorial.com/example?topicId=252\",\"WARC-Payload-Digest\":\"sha1:SMTIOFIDRZFXQHRT3UOXIDQMRKDPFOOK\",\"WARC-Block-Digest\":\"sha1:7DCXRVUCN4BAUDP3WHT2XUHFNLMV5NKA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587606.8_warc_CC-MAIN-20211024204628-20211024234628-00018.warc.gz\"}"}