Python Forum
how to create pythonic codes including for loop and if statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to create pythonic codes including for loop and if statement?
#1
I am a newbie on python coding. Below are my legacy python codes.

for i, df_list in enumerate(df_list_of_list):
    for j, df in enumerate(df_list):
        if i == 0 & j ==0:
            fred.writeCsv2Hdfs('earnings.csv', df)  # create file successfully
        else:    
            fred.appendCsv2Hdfs('earnings.csv', df) # append file successfully
        
        df.to_csv('outputs/earnings.csv', mode='a', index_label='date', header=(i==0|j==0))   # writing results onto local file
But I want to change these codes to more pythonic codes. First, I tried to use lambda statement like below,

def writeDFHdfs(filename, df_list):
    f = lambda i, df: fred.writeCsv2Hdfs(filename, df) if i == 0 else fred.appendCsv2Hdfs(filename, df), df_list
But I am afraid I have no idea here in generating pythonic codes including both for loop and if statement. How can these multi line codes be changed to one or two lines? Any reply will be really thankful.
Reply
#2
The only unpythonic parts that I see here are if i==0 & j==0 which should be if not(i or j) and i==0|j==0 which should be not(i and j). Note that & and | are bitwise operators and not logical operators. The result is also different because they have different levels of operator precedence. For example
>>> 0==0 & 7==0
True
>>> 0==0 and 7==0
False
Writing code on a single line does not necessarily give better Python code, and it is not necessarily 'pythonic'. I'm not sure there is a better solution here.
aupres likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
Photo Python code: While loop with if statement HAMOUDA 1 535 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,405 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Multiply and Addition in the same loop statement with logic. joelraj 2 998 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Including data files in a package ChrisOfBristol 4 2,463 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,516 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Create Dynamic For Loop quest 3 4,290 Apr-26-2021, 02:03 PM
Last Post: ibreeden
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,761 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Python Matplotlib: How do I save (in pdf) all the graphs I create in a loop? JaneTan 1 8,778 Feb-28-2021, 06:20 PM
Last Post: Larz60+
  if statement in for loop researcher123 6 2,506 Oct-01-2020, 05:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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