Home > Back-end >  Convert a dictionary to accessible array in Python
Convert a dictionary to accessible array in Python

Time:07-07

How can I convert the following dictionary which contains an array within an array: to an array easily so I can access for example array[0]

{'New Beton': [`'C:\\User\\New map\\Test1.jpg',`'C:\\User\\New map\\Test2.jpg', 'C:\\User\\New map\\Test3.jpg']}

Which I need to convert to

New Beton = ["C:\\User\\New map\\Test1.jpg", "C:\\User\\New map\\Test2.jpg", "C:\\User\\New map\\Test3.jpg"]

CodePudding user response:

Just access it directly.

you_dict['New Beton'][0]

And make sure your variable names don't have whitespace. I think except 'Rockstar' no language allows that. also, you seem to gave mixed in some backticks in your example.

CodePudding user response:

Do you want to convert dictionary into a nested list? Then, something like this will work.

def convert(d):
    l = []
    for k, v in d.items():
        l.append(v)
    return l

d = {'foo':['bar', 'baz']}
l = convert(d)
print(l[0])

but there are better ways to get that value without creating a list. it'd ve great if you could share more details about what you want to do so that i can give you specific examples.

  • Related