How To Use List Comprehension In Python

Поделиться
HTML-код
  • Опубликовано: 14 окт 2024

Комментарии • 11

  • @ZAR_KoWaLsKy_
    @ZAR_KoWaLsKy_ 7 месяцев назад +4

    Nice explanation! Trully useful for adding some extra spice to my projects. Keep up the good work buddy!

  • @PhrontDoor
    @PhrontDoor 5 месяцев назад +3

    Loved the presentation ... very clear.
    Also could use :
    itemArr = [ f'item{n}' for n in range(3) ] to avoid the str cast

    • @igorthelight
      @igorthelight 27 дней назад

      Python will still cast n to a string. Just implicitly.
      Correct me if I'm worong

  • @droid812
    @droid812 3 месяца назад +1

    Very clever on zooming the code, that was a great detail
    Question: Is it recomended to use this comprehension? i mean it kinda affects readability but, what are good uses for it?

    • @TaylorsSoftware
      @TaylorsSoftware  3 месяца назад

      Great question! You should use list comprehensions when you want to quickly loop through or generate a list with minimal conditional statements. You should avoid using it when you have multiple conditions. It will make troubleshooting and readability more difficult with too many conditions.

  • @fusebox9725
    @fusebox9725 2 месяца назад +1

    perhaps we can also use append function as i.append("new") for i in itemList)

  • @igorthelight
    @igorthelight 27 дней назад

    Also, list comprehensions are faster!

  • @jurgenblick5491
    @jurgenblick5491 5 месяцев назад

    which version of py