Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list comprehension
#1
Hi! I try to modify the function below with list comprehation.
But I don't now how to write that else secvantion

This:
def cakes(recipe, available):
    list_value = [ ]
    for key,value in recipe.items():
        if key in available:
            a = int(available[key]) // int(value)
            list_value.append(a)
        else:
            return 0
    list_value.sort()
    return list_value[0]  
In this:
def cakes(recipe, available):
    list_value = [int(available[key]) // int(value) for key,value in recipe.items() if key in available else return 0  ].sort()
    return list_value[0] 
Reply
#2
def cakes(recipe, available):
    values = [int(available[key]) // int(value) for key, value in recipe.items() if key in available]
    if len(values) > 0:
        values.sort()
        return values[0]
    return 0
3lnyn0 likes this post
Reply
#3
Quote:[int(available[key]) // int(value) for key,value in recipe.items() if key in available else return 0 ].sort()

The sort method of a list, does not return the list. Instead, the list is sorted inline and returns None, which is assigned to list_value.

You can wrap around the whole list comprehension with the sorted function. The sorted function returns a new list, where the original list is not modified.

If you want to get the highest number of a sequence, use the max function.
To get the lowest number, use min.

Example:
from __future__ import annotations


def cakes(recipe: dict, available: dict) -> int | None:

    values = [
        int(available[key]) // int(value)
        for key, value in recipe.items()
        if key in available
    ]

    if values:
        return min(values)
Annotations: They are not mandatory, but can give you a hint, which types should be used and which types return. For example, an empty recipe will return None. A valid recipe will return an int.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
You can avoid checking if values is empty by using the 'default' argument in min()
>>> min([], default=0)
0
So
def cakes(recipe, available):
    return min(
        (int(available[key]) // int(value) for key, value in recipe.items() if key in available),
        default=0)
Reply
#5
@Gribouillis, thanks.

The blacked Version is easier to read:
def cakes(recipe, available):
    return min(
        (
            int(available[key]) // int(value)
            for key, value in recipe.items()
            if key in available
        ),
        default=0,
    )
The parenthesis for the Generator Expression is not mandatory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 506 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,246 Apr-02-2023, 06:31 PM
Last Post: tester_V
  List comprehension used differently coder_sw99 3 1,710 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,824 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,235 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,212 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,574 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,133 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 2,774 Sep-25-2020, 12:14 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020