-3

My list looks like the following:
['https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 37.pdf', 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2020/BAK CAN CER 34.pdf', 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 38 FERC 3120 BAK US.pdf']

And I want to grab each of the CER # off the end of each element and create a dictionary that has the CER # as the key and the entire element as the corresponding output.

For example, the dictionary would look like the following:
tariffs = {'CER 37': 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 37.pdf', etc.}

So if I look up the tariff name, it will output me the correct PDF.

martineau
  • 119,623
  • 25
  • 170
  • 301
Amelia
  • 3
  • 1

2 Answers2

0
 tariff_dict = {value[value.find(" CER ")+1:(value.find(" CER ")+7)]: value for value in my_list}
Emerson Maki
  • 44
  • 1
  • 5
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Buddy Bob Jun 11 '21 at 00:19
0
lst = [
    'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 37.pdf',
    'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2020/BAK CAN CER 34.pdf',
    'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 38 FERC 3120 BAK US.pdf'
    ]
dictionary = {}
for item in lst:
    splitted = item.split(' ')
    index = splitted.index('CER')
    key = f'{splitted[index]} {splitted[index + 1].replace(".pdf", "")}'
    dictionary[key] = item
print(dictionary)

returns

{
    'CER 37': 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 37.pdf',
    'CER 34': 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2020/BAK CAN CER 34.pdf',
    'CER 38': 'https://www.enbridge.com/Projects-and-Infrastructure/For-Shippers/Tariffs/Enbridge-Bakken-Pipeline-Company-Inc-Bakken-Canada-tariffs.aspx/~/media/Enb/Documents/Tariffs/2021/BAK CAN CER 38 FERC 3120 BAK US.pdf'
}
K.Mat
  • 1,341
  • 11
  • 17