Skip to content Skip to sidebar Skip to footer

Python List Into Dict

Here is my issue. I have a list like this (out of a .txt file I have already assigned to a list element): sharenames = ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American A

Solution 1:

You can create a mapping from a list of tuples:

data =  ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American Airlines Group Inc.', 'ROST:Ross Stores Inc.', 'VRTX:Vertex Pharmaceuticals Incorporated', 'WDC:Western Digital Corp', 'NCLH:Norwegian Cruise Line Holdings Ltd', 'SWKS:Skyworks Solutions Inc.', 'BBBY:Bed Bath & Beyond Inc.', 'BIDU:Baidu Inc (ADR)', 'ENDP:Endo International plc', '"FRA:DBK":Deutsche Bank AG', '"FRA:FME":Fresenius Medical Care AG & Co. KGaA', '"FRA:DAI":Daimler AG']
mapping = dict(entry.rsplit(':', 1) for entry in data)

Notice I'm using string.rsplit to the maximum of a single split (second argument). Also, this assumes you have no colons in your values.

Items in mapping:

ENDP -> Endo International plc
WDC -> Western Digital Corp
VIAB -> Viacom Inc.
AAL -> American Airlines Group Inc.
DLTR -> Dollar Tree Inc.
BBBY -> Bed Bath & Beyond Inc.
"FRA:DAI"-> Daimler AG
VRTX -> Vertex Pharmaceuticals Incorporated
"FRA:FME"-> Fresenius Medical Care AG & Co. KGaA
SWKS -> Skyworks Solutions Inc.
NCLH -> Norwegian Cruise Line Holdings Ltd
ROST -> Ross Stores Inc.
"FRA:DBK"-> Deutsche Bank AG
BIDU -> Baidu Inc (ADR)

To clean keys from double-quotes you can do:

clean_mapping = {k.replace('"',''): v for k, v in mapping}

Solution 2:

This might do what you want:

sharenames = ['VIAB:Viacom Inc.', 'DLTR:Dollar Tree Inc.', 'AAL:American Airlines Group Inc.', 'ROST:Ross Stores Inc.', 'VRTX:Vertex Pharmaceuticals Incorporated', 'WDC:Western Digital Corp', 'NCLH:Norwegian Cruise Line Holdings Ltd', 'SWKS:Skyworks Solutions Inc.', 'BBBY:Bed Bath & Beyond Inc.', 'BIDU:Baidu Inc (ADR)', 'ENDP:Endo International plc', '"FRA:DBK":Deutsche Bank AG', '"FRA:FME":Fresenius Medical Care AG & Co. KGaA', '"FRA:DAI":Daimler AG']

# Conversions;
sharenames = (sharename.rsplit(':',1) for sharename in sharenames)
sharenames = ((sharename[0].strip('"'),sharename[1]) for sharename in sharenames)
sharenames = dict(sharenames)

print sharenames['ROST']
print sharenames["FRA:FME"]

Solution 3:

You need to split every item at the ":". The following code makes the desired dictionary:

sharenames = {}
for item in lst:
    sharenames[":".join(item.split(":")[:-1])] = item.split(":")[-1]

Example: sharenames["ENDP"] >>> 'Endo International plc'

Solution 4:

d = {}
for e in sharenames:
    t = tuple(string.rsplit(e,':', 1))
    d[re.sub('\"','',t[0])] = t[1]

for k,v in d.iteritems():
    print k,v

ENDP Endo International plc

WDC Western Digital Corp

VIAB Viacom Inc.

FRA:DBK Deutsche Bank AG

AAL American Airlines Group Inc.

DLTR Dollar Tree Inc.

BBBY Bed Bath & Beyond Inc.

VRTX Vertex Pharmaceuticals Incorporated

SWKS Skyworks Solutions Inc.

NCLH Norwegian Cruise Line Holdings Ltd

ROST Ross Stores Inc.

FRA:DAI Daimler AG

FRA:FME Fresenius Medical Care AG & Co. KGaA

BIDU Baidu Inc (ADR)

Post a Comment for "Python List Into Dict"