0

I have the following code:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(10)]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [names, surnames, email, salary, gender, age]

myDict = {}
for i in range(6):
    myDict[list_of_keys[i]] = list_of_lists[i]

for i in myDict:
    print(i,': ', myDict[i])

That has the following output

names :  ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames :  ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email :  ['Mimi_Perez@email.com', 'Monique_Gomez@email.com', 'Derick_Sanchez@email.com', 'Pierre_Iglesias@email.com', 'Sara_Casado@email.com', 'Marti_Mata@email.com', 'Isabel_Li@email.com', 'Elicia_Perez@email.com', 'Dani_Li@email.com', 'Bell_Gomez@email.com']
salary :  [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender :  ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age :  [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

I would like to create the dictionary without having to manually write the variables 'list_of_keys' and 'list of lists'.

I also would like to use a list comprehension instead of a for loop, but I dont know how to do it when there is an '=' sign in the for loop.

Thank you

Arman Mojaver
  • 352
  • 1
  • 9
  • what is your expected output? – fireball.1 May 02 '20 at 12:31
  • Why do you start out with those list variable and don't use a dict straight away? – user2390182 May 02 '20 at 12:36
  • The expected output is the one that is in the question. I want to do exactly the same, with a more efficient and compact code. In this case I have 6 lists, but if I had 600 it would take me a long time to write the variables 'list_of_keys' and 'list_of_lists'. There must be a better way to do this. Thank you – Arman Mojaver May 02 '20 at 12:37
  • Starting a dict straight away is an interesting option, but I would like to know how to convert between a group of lists to a dictionary because I want to learn more about this, and in case I already have the information in lists and I don't want to edit the lists to convert them into a dictionary. – Arman Mojaver May 02 '20 at 12:41

3 Answers3

1

You can't avoid the creation of the two lists but you can remove the loop and the dictionary init:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(10)]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [names, surnames, email, salary, gender, age]

# Dictionary comprehension
myDict = {k: v for (k, v) in zip(list_of_keys, list_of_lists)}

print(myDict)

Or even simpler with the dict init:

myDict = dict(zip(list_of_keys, list_of_lists))

See dictionary documentation fr more details about how to init.

Louis Lac
  • 5,298
  • 1
  • 21
  • 36
1

You somehow need to define the key or 'name' that is associated to each list in your dict. Using the name of the variable is not a good idea, as it is only a reference to each list object. However, it is possible, but highly discouraged. See here.

If the number of lists is not very large, you could simply define your dict directly:

names = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
surnames = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
email = [names[i] + '_' + surnames[i] + '@email.com' for i in range(len(names))]
salary = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
gender = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
age = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]

my_data = {
    "names": names,
    "surnames": surnames,
    "email": email,
    "salary": salary,
    "gender": gender,
    "age": age,
}

# Or simply define the lists inside the dictionary


my_data = {
    "names": ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell'],
    "surnames": ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez'],
    "salary": [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000],
    "gender": ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F'],
    "age": [31, 33, 30, 31, 34, 34, 31, 31, 32, 30],
}
# Email depends on the size of the other lists, so we add it afterwards
my_data["email"] = [
    names[i] + "_" + surnames[i] + "@email.com" for i in range(len(my_data["names"]))
]

Is that what you are actually trying to do? Or do you want a list of dicts to store each employee so that you can access them like employee[0]['name'] -> 'Mimi', etc.?

If the number of lists is large, I would recommend the second approach, as the structure is clear from the code, and you don't need to repeat the names of the lists and therefore have the cleanest, DRYest and shortest code.

my_data = {}
my_data["names"] = ['Mimi', 'Monique', 'Derick', 'Pierre', 'Sara', 'Marti', 'Isabel', 'Elicia', 'Dani', 'Bell']
my_data["surnames"] = ['Perez', 'Gomez', 'Sanchez', 'Iglesias', 'Casado', 'Mata', 'Li', 'Perez', 'Li', 'Gomez']
my_data["salary"] = [16000, 15000, 16000, 15000, 15000, 16000, 16000, 15000, 16000, 17000]
my_data["gender"] = ['F', 'F', 'M', 'M', 'F', 'M', 'F', 'F', 'M', 'F']
my_data["age"] = [31, 33, 30, 31, 34, 34, 31, 31, 32, 30]
my_data["email"] = [
    my_data["names"][i] + "_" + my_data["surnames"][i] + "@email.com" for i in range(len(my_data["names"]))
]

import pandas
df = pandas.DataFrame.from_dict(my_data)
cybot
  • 101
  • 4
  • Thank you for your input. I have heard that using variable names is not a good idea, but I don't fully understand why. As I said in a previous comment, I want to store the data in a pandas dataframe, and I need the elements of 'list_of_keys' to be the labels of the columns. – Arman Mojaver May 02 '20 at 14:11
  • Ah, I did not read the comment about pandas. I have edited my answer to create a DataFrame with the 'list_of_keys' as the column labels. One of the reasons you don't want to use variable names as your column names is the restriction on naming variables in Python. `E-Mail`, `Full Name` and `#Phone` would not work for example. Yet you can use them if you follow the method I showed in the last code example. – cybot May 02 '20 at 15:03
1

Let's say you have 600 list of keys and values,when you execute the program those data should be come as a parameters or it should be defined in the script.For example it is defined in the script or it could be results from a database.Somehow you need to define at least one list or otherwise you need to access the globals and get the defined variables and write some filtering method.

If you defined the list_of_keys,then you can use eval method to get the mapping object of the defined variables.

list_of_keys = ['names', 'surnames', 'email', 'salary', 'gender', 'age']
list_of_lists = [eval(i) for i in list_of_keys]

result = dict(zip(list_of_keys, list_of_lists))

If you want to get defined variables from the globals,you can start from this way

g_keys = [item for item in dir() if (not item.startswith("__") and not item.startswith("_"))]
for k in ks:
    if isinstance(eval(k),list):
        print(k)
        // you have to find a way to remove unwanted values
Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24
  • Thanks for this. list_of_lists = [eval(i) for i in list_of_keys]. It will improve the code. – Arman Mojaver May 02 '20 at 14:05
  • The end purpose of this is to put it in a pandas dataframe. I know that I can get the data and set the column names with a list with one line of code,, but I wanted to learn how to do it this way. – Arman Mojaver May 02 '20 at 14:08