Dictionary with multiple values in Python

I am trying to improve a code that I’ve made. The idea is to automate some data processing.
The idea is to link two dictionaries so that I can “easily” move from key/values and format outputs.

I now have :

my_first_dict = {"value_1": "value_1_formatted", value_2: "value_2_formatted", ...}
my_second_dict = {"value_1_formatted": "value_1_formatted_differently", "value_2_formatted": "value_2_formatted_differently", ...")

Doing so allows me to identify and rename as I want using key/value pairs. However, it can sometimes make code heavier and less readable.

I’d like to know if there is a more efficient way to proceed. I was wondering if, for example, we could have a “three layer dictionary”, with values/keys accessible via : .keys() or .values()[...].

That would look like :

my_dict = {"value_1": "value_1_formatted": "value_1_formatted_differently"}

Where my_dict["value_1"].values()[1] would return "value_1_formatted_differently" (it doesn’t work, it’s just to present my idea of it)

What do you think about ?

5

That “three layer dictionary” syntax is obviously not valid, but you can achieve the same idea by just doing a nested lookup

>>> my_second_dict[my_first_dict["value_1"]]
'value_1_formatted_differently'

In other words, starting with your key first look up the value from the first dict, then use that as the key for the next lookup.

2

If "value_1" is your common name, true key and "value_1_formatted" and "value_1_formatted_differently" are aliases that you would like to uncover based on the key/common name, then I would likely use something like this nested dictionary:

my_lookup = {
    "Jonny": {
        "formatted": "Jon",
        "formatted_differently": "Jonathan"
    },
    "Annie": {
        "formatted": "Anna",
        "formatted_differently": "Anastasia"
    },
}

Then you can:

key = "Annie"
print(my_lookup[key]["formatted_differently"])

This also might allow you to have incomplete data. For example:

my_lookup = {
    "Jonny": {
        "formatted": "Jon",
        "formatted_differently": "Jonathan"
    },
    "Annie": {
        "formatted": "Anna",
        ##"formatted_differently": "Anastasia"   <--- Now missing
    },
}

Then

key = "Annie"
print(my_lookup[key].get("formatted_differently", key))

1

I would suggest using 2D array, e.g. list of lists to store 2 different formats for each key, for example

columns = {"column1":0,"column2":1,"column3":2}  # associate names with 0-based column names
rows = {"firstformat":0,"secondformat":1}  # associate names with 0-based row names
array = [["value1format1","value2format1","value3format1"],["value1format2","value2format2","value3format2"]]  # each sublist is one row
print(array[rows["secondformat"]][columns["column3"]])  # output is value3format2

If you are allowed to use external packages you might pandas.DataFrame for that purpose following way

import pandas as pd
df = pd.DataFrame([["value1format1","value2format1","value3format1"],["value1format2","value2format2","value3format2"]],columns=("column1","column2","column3"),index=("firstformat","secondformat"))
print(df["column3"]["secondformat"])

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật