Django: for loop and if condition in one lineDoes Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Django scale?How to debug in Django, the good way?How to read a file line-by-line into a list?Iterating over dictionaries using 'for' loopsHow to print the full traceback without halting the program?Catch multiple exceptions in one line (except block)UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)How do I write JSON data to a file?

Why don't countries like Japan just print more money?

Can the pre-order traversal of two different trees be the same even though they are different?

How could a tree survive a volcanic blast?

What is the oldest commercial MS-DOS program that can run on modern versions of Windows without third-party software?

What type of tests you should do first?

What was the first third-party commercial application for MS-DOS?

What does it cost to buy a tavern?

Is there any proof that high saturation and contrast makes a picture more appealing in social media?

Why don't we have a weaning party like Avraham did?

How can I prevent a user from copying files on another hard drive?

Is "Busen" just the area between the breasts?

Is it possible to transpose samples (in cents) from minor to major?

Text alignment in tikzpicture

Definition of 'vrit'

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

Cut the gold chain

Dmesg full of I/O errors, smart ok, four disks affected

Umlaut character order when sorting

Time at 1 g acceleration to travel 100 000 light years

What triggered jesuits' ban on infinitesimals in 1632?

Boss wants someone else to lead a project based on the idea I presented to him

Second 100 amp breaker inside existing 200 amp residential panel for new detached garage

80s or 90s Fantasy novel, part of series. Castle talks to wizard, 2 headed dragon fights itself

What does "determined" refer to in Acts 17v26?



Django: for loop and if condition in one line


Does Python have a ternary conditional operator?Accessing the index in 'for' loops?Does Django scale?How to debug in Django, the good way?How to read a file line-by-line into a list?Iterating over dictionaries using 'for' loopsHow to print the full traceback without halting the program?Catch multiple exceptions in one line (except block)UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 20: ordinal not in range(128)How do I write JSON data to a file?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to bring if dynamic_tickets: and for ticket in dynamic_tickets: in one line. I always receive a syntax error. Do you have an idea, how to achieve that?



def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]

for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if ticket.dynamic_pricing_activated()
]
if dynamic_tickets:
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")









share|improve this question






















  • I always receive a syntax error. What error do you get? and on what line?

    – DirtyBit
    Mar 25 at 7:04












  • I think OP is asking for the python equivalent of the JS array && array.forEach()

    – ManavM
    Mar 25 at 7:06






  • 1





    to solve this error you must add stack trace of raised exception

    – vorujack
    Mar 25 at 7:13











  • What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

    – Joey Coder
    Mar 25 at 7:16











  • you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

    – Woods Chen
    Mar 25 at 7:36

















0















I want to bring if dynamic_tickets: and for ticket in dynamic_tickets: in one line. I always receive a syntax error. Do you have an idea, how to achieve that?



def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]

for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if ticket.dynamic_pricing_activated()
]
if dynamic_tickets:
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")









share|improve this question






















  • I always receive a syntax error. What error do you get? and on what line?

    – DirtyBit
    Mar 25 at 7:04












  • I think OP is asking for the python equivalent of the JS array && array.forEach()

    – ManavM
    Mar 25 at 7:06






  • 1





    to solve this error you must add stack trace of raised exception

    – vorujack
    Mar 25 at 7:13











  • What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

    – Joey Coder
    Mar 25 at 7:16











  • you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

    – Woods Chen
    Mar 25 at 7:36













0












0








0








I want to bring if dynamic_tickets: and for ticket in dynamic_tickets: in one line. I always receive a syntax error. Do you have an idea, how to achieve that?



def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]

for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if ticket.dynamic_pricing_activated()
]
if dynamic_tickets:
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")









share|improve this question














I want to bring if dynamic_tickets: and for ticket in dynamic_tickets: in one line. I always receive a syntax error. Do you have an idea, how to achieve that?



def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]

for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if ticket.dynamic_pricing_activated()
]
if dynamic_tickets:
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")






python django






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 7:03









Joey CoderJoey Coder

414113




414113












  • I always receive a syntax error. What error do you get? and on what line?

    – DirtyBit
    Mar 25 at 7:04












  • I think OP is asking for the python equivalent of the JS array && array.forEach()

    – ManavM
    Mar 25 at 7:06






  • 1





    to solve this error you must add stack trace of raised exception

    – vorujack
    Mar 25 at 7:13











  • What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

    – Joey Coder
    Mar 25 at 7:16











  • you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

    – Woods Chen
    Mar 25 at 7:36

















  • I always receive a syntax error. What error do you get? and on what line?

    – DirtyBit
    Mar 25 at 7:04












  • I think OP is asking for the python equivalent of the JS array && array.forEach()

    – ManavM
    Mar 25 at 7:06






  • 1





    to solve this error you must add stack trace of raised exception

    – vorujack
    Mar 25 at 7:13











  • What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

    – Joey Coder
    Mar 25 at 7:16











  • you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

    – Woods Chen
    Mar 25 at 7:36
















I always receive a syntax error. What error do you get? and on what line?

– DirtyBit
Mar 25 at 7:04






I always receive a syntax error. What error do you get? and on what line?

– DirtyBit
Mar 25 at 7:04














I think OP is asking for the python equivalent of the JS array && array.forEach()

– ManavM
Mar 25 at 7:06





I think OP is asking for the python equivalent of the JS array && array.forEach()

– ManavM
Mar 25 at 7:06




1




1





to solve this error you must add stack trace of raised exception

– vorujack
Mar 25 at 7:13





to solve this error you must add stack trace of raised exception

– vorujack
Mar 25 at 7:13













What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

– Joey Coder
Mar 25 at 7:16





What I see when I try for ticket in dynamic_tickets if dynamic_tickets: is that message in my console SyntaxError: invalid syntax

– Joey Coder
Mar 25 at 7:16













you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

– Woods Chen
Mar 25 at 7:36





you can only use for ticket in dynamic_tickets if ticket but not for ticket in dynamic_tickets if dynamic_tickets. there's no need to write a list comprehension for the second one.

– Woods Chen
Mar 25 at 7:36












1 Answer
1






active

oldest

votes


















2














No need to use 'if dynamic_tickets:' , because when the list 'dynamic_tickets' will not be empty then only for loop will execute.
just write the code like this:



def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]

for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if
ticket.dynamic_pricing_activated()
]

for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332661%2fdjango-for-loop-and-if-condition-in-one-line%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    No need to use 'if dynamic_tickets:' , because when the list 'dynamic_tickets' will not be empty then only for loop will execute.
    just write the code like this:



    def adjust_prices():
    events = Event.objects.filter(status=EventStatus.LIVE)
    active_events = [event for event in events if not event.is_over]

    for active_event in active_events:
    dynamic_tickets = [
    ticket for ticket in active_event.tickets.all() if
    ticket.dynamic_pricing_activated()
    ]

    for ticket in dynamic_tickets:
    print(ticket)
    print("DO OTHER STUFF")





    share|improve this answer





























      2














      No need to use 'if dynamic_tickets:' , because when the list 'dynamic_tickets' will not be empty then only for loop will execute.
      just write the code like this:



      def adjust_prices():
      events = Event.objects.filter(status=EventStatus.LIVE)
      active_events = [event for event in events if not event.is_over]

      for active_event in active_events:
      dynamic_tickets = [
      ticket for ticket in active_event.tickets.all() if
      ticket.dynamic_pricing_activated()
      ]

      for ticket in dynamic_tickets:
      print(ticket)
      print("DO OTHER STUFF")





      share|improve this answer



























        2












        2








        2







        No need to use 'if dynamic_tickets:' , because when the list 'dynamic_tickets' will not be empty then only for loop will execute.
        just write the code like this:



        def adjust_prices():
        events = Event.objects.filter(status=EventStatus.LIVE)
        active_events = [event for event in events if not event.is_over]

        for active_event in active_events:
        dynamic_tickets = [
        ticket for ticket in active_event.tickets.all() if
        ticket.dynamic_pricing_activated()
        ]

        for ticket in dynamic_tickets:
        print(ticket)
        print("DO OTHER STUFF")





        share|improve this answer















        No need to use 'if dynamic_tickets:' , because when the list 'dynamic_tickets' will not be empty then only for loop will execute.
        just write the code like this:



        def adjust_prices():
        events = Event.objects.filter(status=EventStatus.LIVE)
        active_events = [event for event in events if not event.is_over]

        for active_event in active_events:
        dynamic_tickets = [
        ticket for ticket in active_event.tickets.all() if
        ticket.dynamic_pricing_activated()
        ]

        for ticket in dynamic_tickets:
        print(ticket)
        print("DO OTHER STUFF")






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 25 at 9:40









        sascha

        87231025




        87231025










        answered Mar 25 at 9:18









        Aman PrajapatiAman Prajapati

        10215




        10215





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332661%2fdjango-for-loop-and-if-condition-in-one-line%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해