0

here is my txt file that has contained all of the lines. What I want to do is create a dictionary, and access a key, and get a list of values

Finance:JPMC
Software:Microsoft
Conglomerate:L&T
Conglomerate:Amazon
Software:Palantir
Defense:BAE
Defense:Lockheed
Software:TCS
Retail:TjMax
Retail:Target
Oil:Exxon
Oil:Chevron
Oil:BP
Oil:Gulf
Finance:Square
FMCG:PnG
FMCG:JohnsonNJohnson
FMCG:Nestle
Retail:Sears
Retail:FiveBelow
Defense:Boeing
Finance:Citadel
Finance:BridgeWater
Conglomerate:GE
Conglomerate:HoneyWell
Oil:ONGC
FMCG:Unilever
Semiconductor:Intel
Semiconductor:Nvidia
Semiconductor:Qualcomm
Semiconductor:Microchip
Conglomerate:Samsung
Conglomerate:LG
Finance:BoA
Finance:Discover
Software:TCS
Defense:Raytheon
Semiconductor:Microsemi
Defense:BAE
Software:Meta
Oil:SinoPec
Defense:Saab
Defense:Dassault
Defense:Airbus
Software:Adobe
Semiconductor:TSMC
FMCG:CocoCola
FMCG:Pesico
Retail:Kohls

Here is my attempted code

f = open("companyList.txt", "r")
sector, company = [], []
for line in f:
    first, second = line.split(":")
    sector.append(first)
    company.append(second)




dictionary = {}


for key in sector:
    for element in company:
        dictionary[sector].append(element)

print(dictionary)

Since there are multiple duplicate keys, I wanted to append a list to that particular key as python doesn't allow duplicate keys.

spidoinky
  • 3
  • 1
  • check [here](https://stackoverflow.com/questions/10664856/make-a-dictionary-with-duplicate-keys-in-python) – Kofi Jan 15 '22 at 16:59
  • use a [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) – mozway Jan 15 '22 at 17:00

3 Answers3

1

If i understand your question right you can do this:

from collections import defaultdict

dictionary = defaultdict(list)
for line in f:
    first, second = line.split(":")
    dictionary[first].append(second)
Bemwa Malak
  • 1,182
  • 1
  • 5
  • 18
  • This would be amazing to use, but unfortunately, I'm only allowed specific material, and I haven't been able to use defaultdict – spidoinky Jan 15 '22 at 17:28
0

you should do:

f = open("companyList.txt", "r")
sector, company = [], []
for line in f:
    first, second = line.split(":")
    sector.append(first)
    company.append(second)

dictionary = {}


for sectory,companyy in zip(sector,company):
    dictionary[sectory] = companyy
    
    
    for key in sector:
        dictionary[sector] = key
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
0

I think this is what you want:

pairs = {}

with open("tst.txt", "r") as f:
    while True:
        line = f.readline().strip()

        if not line:
            break

        sector, value = line.split(":", 1)

        if sector not in pairs:
            pairs[sector] = []

        pairs[sector].append(value)
        
    f.close()

print(pairs)

Chris
  • 26,361
  • 5
  • 21
  • 42
Wrench56
  • 290
  • 2
  • 14
  • I see what you did, that makes a lot of sense, I also didn't really think to use the index values of the .txt file. Thanks! – spidoinky Jan 15 '22 at 17:14
  • Rather than `pair = line.split(":", 1)`, you can unpack the results of `split` into `key` and `value` directly. Then something like `pairs[pair[0]].append(pair[1])` becomes `pairs[key].append(value)`. – Chris Jan 15 '22 at 17:20
  • You are right! I'll settle with sector and value. – Wrench56 Jan 15 '22 at 17:23