Skip to content
Advertisement

How to create a function which Iterates over multiple lists

So I’m creating a series of column mappings, I can do this manually like so

#Produces a list of dictionaries
def create_seed_dict(column, dataset):
    seed_col_vals = dataset[column].values.tolist()
    seed_col_keys =[column] * (len(seed_col_vals))
    seed_col_map = list(zip(seed_col_keys, seed_col_vals))
    seed_dict = []
    for k,v in seed_col_map:
        diction = {}
        diction[k] = v 
        seed_dict.append(diction)
    return seed_dict, seed_col_map

#Create dummy dataframe
num = [1, 2, 3] 
color = ['red', 'white', 'black'] 
value = [255, 256, 257] 
dict = {'Number': num, 'Color': color, 'Score': value}  
df = pd.DataFrame(dict) 

Num_seed_Dict,Num_map = create_seed_dict('Number', df)
print(Num_seed_Dict)
-----------------------------
Out[1]: [{'Number': 1}, {'Number': 2}, {'Number': 3}]

The function produces a mapping of a value and its column. Great, now I want to make this more general.

Currently, if I needed to map 2 columns for example I run the following:

num_vals = df['Number'].values.tolist()
num_keys =['Number'] * (len(num_vals))
num_map = list(zip(num_keys, num_vals))
numDict = []
for k,v in num_map:
    diction = {}
    diction[k] = v 
    numDict.append(diction)
    

color_vals = df['Color'].values.tolist()
color_keys =['Color'] * (len(color_vals))
color_map = list(zip(color_keys, color_vals))
colorDict = []
for k,v in num_map:
    diction = {}
    diction[k] = v 
    colorDict.append(diction)

colorNum_Dict = []
for (f,b) in zip(num_map, color_map):
    diction = {}
    diction[f[0]] = f[1] 
    diction[b[0]] = b[1] 
    colorNum_Dict.append(diction)
    
print(colorNum_Dict)
-----------------------------
[{'Number': 1, 'Color': 'red'}, {'Number': 2, 'Color': 'white'}, {'Number': 3, 'Color': 'black'}]

Works as well but not ideal if I have a lot of columns. My question is how do I make the first function valid for more than 1 column?

My main issue is that the for (f,b) in zip(num_map, color_map): line, I dont know how to dynamically define (f,b..) to match the number of _maps I have within zip

Advertisement

Answer

You just have to accept them in a tuple. The Pythonic way you create an arbitrary quantity of variables is with a sequence. zip already returns a list for you.

a = [1, 2, 3]
b = [True, False, False]
c = ['x', 'y', 'z']
abc = [a, b, c]

for several in zip(*abc):
    print(several)

Output:

(1, True, 'x')
(2, False, 'y')
(3, False, 'z')

Thereafter, you can use len(several) as the controlling value to work with those items. If what you want is to use the first element as a key, and the rest as the value:

diction[several[0]] = lsit(several[1:])

If you want to enter each element as a key, with the others as values, just iterate through several, using the indicated element as the key, and the others as the value. If you’re unclear how to do that, look up slice notation, or how to remove one element from a sequence.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement