0

I have two lists. I would like to concatenate in order to get a list of strings. The first list is composed of strings The second list is composed of a Timestamp.

I would concatenate as follow in one line with a list of comprehension.

#just for creating something similar to my actual situation
import pandas as pd
quarter=["First Quarter","Second Quarter","Third Quarter","Fourth Quarter"]
quarter_forecast_day=pd.date_range(start="12/15/2020",periods=4)

I would like something like that:

str(quarter[x]+'\n'+quarter_forecast[y].strftime("%d/%m/%Y"))

What I tried:

#quarter_label=[str(quarter[x]+'\n'+y.strftime("%d/%m/%Y")) for x,y in [quarter, quarter_forecast_day]]
#too many values to unpack (expected 2)

#quarter_label=[str(quarter[x]+'\n'+y.strftime("%d/%m/%Y")) for x,y in (quarter, quarter_forecast_day)]
#too many values to unpack (expected 2)

#quarter_label=[str(quarter[x]+'\n'+y.strftime("%d/%m/%Y")) for x,y in quarter, quarter_forecast_day]
#invalid sintax

#quarter_label=[lambda x,y:str(quarter[x]+'\n'+y.strftime("%d/%m/%Y") for x,y in [quarter, quarter_forecast]]
#invalid sintax

quarter_label=[lambda x,y:str(quarter[x]+'\n'+y.strftime("%d/%m/%Y") for x,y in (quarter, quarter_forecast)]
#invalid sintax

Before asking I read this question, this one and this one. But I am still struggling to find a proper way to apply it.

Andrea Ciufo
  • 359
  • 1
  • 3
  • 19

2 Answers2

2

Try with zip:

quarter_label = [q+'\n'+qf.strftime("%d/%m/%Y") for q, qf in zip(quarter, quarter_forecast_day)]

>>> print(quarter_label)

['First Quarter\n15/12/2020', 'Second Quarter\n16/12/2020', 'Third Quarter\n17/12/2020', 'Fourth Quarter\n18/12/2020']
Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • I was curious to understand why `\n` is read as `\n` and not as a command to start a new line? About this point, I was not explicit in my question, because I was not aware that It will return a character I was sure about "Start New Line" command. – Andrea Ciufo Dec 15 '20 at 18:23
  • 1
    `'\n'` is a "Start New Line" command, but when printing the list, the `repr` of the strings is printed and not the `str`. To see the newline, do something like `for s in quarter_label: print(s)`. Also, see this SO question about [the difference between `__repr__` and `__str__`](https://stackoverflow.com/q/1436703/14160477). – Roy Cohen Dec 15 '20 at 18:37
  • @RoyCohen wow it works when I do ´[print(s) for s in quarter_label ]´ And It was something I did not know! – Andrea Ciufo Dec 16 '20 at 10:18
1

First, the str() is not needed as you are concatenating 3 strings (strftime returns a string).

Second, for a double variable comprehension list, you need to use two for loops, as followed:

quarter_label = [quarter[x] + '\n' + quarter_forecast[y] for x in range(len(quarter)) for y in range(len(quarter_forecast))]

The problem with your code is that you're iterating on the items of the list, but you're using them as indices in the expression. With an item iteration:

quarter_label = [q + '\n' + f for q in quarter for f in quarter_forecast]

Results:

['First Quarter\n15/12/2020', 'First Quarter\n16/12/2020', 'First Quarter\n17/12/2020', 'First Quarter\n18/12/2020', 'Second Quarter\n15/12/2020', 'Second Quarter\n16/12/2020', 'Second Quarter\n17/12/2020', 'Second Quarter\n18/12/2020', 'Third Quarter\n15/12/2020', 'Third Quarter\n16/12/2020', 'Third Quarter\n17/12/2020', 'Third Quarter\n18/12/2020', 'Fourth Quarter\n15/12/2020', 'Fourth Quarter\n16/12/2020', 'Fourth Quarter\n17/12/2020', 'Fourth Quarter\n18/12/2020']
  • 1
    I don't think it'll work. When using two for loops inside a list comprehension it's like having two nested for loops, the output will be every possible pair of `q` and `f`. I think the right solution is the one using `zip`. – Roy Cohen Dec 15 '20 at 13:47
  • 2
    depends on what the OP is looking for, I'll add the result in my answer so that they know immediatey what the code does. – Luc Moussiegt Dec 15 '20 at 16:42
  • Thanks for the reply Luc :) The variable quarter_forecast is a TimeStamp is not a String, I have to convert first to a string. I used now `pd.date_range(start="12/15/2020",periods=4)` for creating the example, in my original case I have another TimeStamp datatype and I have to concatenate with a string. :) – Andrea Ciufo Dec 15 '20 at 18:20