Home > database >  Generate a list with only the 2 first values of each entry in another list
Generate a list with only the 2 first values of each entry in another list

Time:10-30

I'm lost on how I should generate a list(dayList) from the values of a list I already have(startTid). I want to only use the 2 first values of every entry. I'm able to "print" out what I want with the code below.

def getDays(filnavn):   #gi en liste med datoer for måneden
startTid, stopTid, forbruk  = getAMSdata(filnavn)
for i in range(len(startTid)):
    entry = startTid[i][0:2]
    print(entry)

#return dayList

getDays(FIL)

CodePudding user response:

You could accumulate the entries into a list:

dayList = []
for i in range(len(startTid)):
    dayList.append(startTid[i][0:2])t
return dayList

But using a list comprehension would be more idiomatic:

return [s[0:2] for s in startTid]
  • Related