Mitch
Mitch

Reputation: 596

How to create a dictionary from two lists of different sizes?

I have a list of symbols:

symbols_list = ['*','†','‡','§','¶','#']

and a longer list of elements as:

note_list = ['a','b','c','d'.....'z']

I want to join them in a dictionary to look like this:

{'*':'a','†':'b','‡':'c','§':'d','¶':'e','#':'f','**':'g','††':'h'...'***':'m'...etc. }

so basically the symbols_list values would repeat as *, **, ***, **** etc.

I tried to just get the symbols_list to be the same length at first using:

for a in range(0,math.ceil(len(note_list)/len(symbols_list))):
   symbols_list.append(symbols_list[a]+symbols_list[a])

but it ended up doubling the elements each iteration instead of just adding one character each time

['*',
 '†',
 '‡',
 '§',
 '¶',
 '#',
 '**',
 '††',
 '‡‡',
 '§§',
 '¶¶',
 '##',
 '****',
 '††††',
 '‡‡‡‡',
 '§§§§',
 '¶¶¶¶',
 '####',
 '********',
 '††††††††',
 '‡‡‡‡‡‡‡‡',
 '§§§§§§§§',
 '¶¶¶¶¶¶¶¶',
 '########',
 '****************']

I thought it would be easier to make the symbols_list the same size first, and then combine them into a dictionary.

Upvotes: 0

Views: 680

Answers (4)

pho
pho

Reputation: 25489

for a in range(0,math.ceil(len(note_list)/len(symbols_list))):
   symbols_list.append(symbols_list[a]+symbols_list[a])

You append symbols_list[a]+symbols_list[a], which essentially doubles symbols_list[a]. You just want to add the first character of symbols_list[a] to itself, so do

for a in range(0,math.ceil(len(note_list)/len(symbols_list))):
   symbols_list.append(symbols_list[a]+symbols_list[a][0])

Upvotes: 1

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

Here is a oneliner:

{ (int(i/len(symbols_list))+1)*symbols_list[i%len(symbols_list)] : e for i, e in enumerate(note_list) }

Upvotes: 0

not_speshal
not_speshal

Reputation: 23146

Maybe do it like this:

new_symbols_list = []
for i in range(len(note_list)//len(symbols_list)+1):
    new_symbols_list += [k*(i+1) for k in symbols_list]
output = {s: l for s, l in zip(new_symbols_list[:len(note_list)], note_list)}

>>> output
{'*': 'a',
 '†': 'b',
 '‡': 'c',
 '§': 'd',
 '¶': 'e',
 '#': 'f',
 '**': 'g',
 '††': 'h',
 '‡‡': 'i',
 '§§': 'j',
 '¶¶': 'k',
 '##': 'l',
 '***': 'm',
 '†††': 'n',
 '‡‡‡': 'o',
 '§§§': 'p',
 '¶¶¶': 'q',
 '###': 'r',
 '****': 's',
 '††††': 't',
 '‡‡‡‡': 'u',
 '§§§§': 'v',
 '¶¶¶¶': 'w',
 '####': 'x',
 '*****': 'y',
 '†††††': 'z'}

Upvotes: 2

fsimonjetz
fsimonjetz

Reputation: 5802

I couldn't resist trying to do it with itertools.

from itertools import chain, count

# yields *, †, ... **, ††, ... infinitely
symbol_generator = chain.from_iterable(((i*x for x in symbols_list) for i in count(1)))

{s:c for s,c in zip(symbol_generator, abc)}
{'*': 'a',
 '†': 'b',
 '‡': 'c',
 '§': 'd',
 '¶': 'e',
 '#': 'f',
 '**': 'g',
 '††': 'h',
 '‡‡': 'i',
 '§§': 'j',
 '¶¶': 'k',
 '##': 'l',
 '***': 'm',
 '†††': 'n',
 '‡‡‡': 'o',
 '§§§': 'p',
 '¶¶¶': 'q',
 '###': 'r',
 '****': 's',
 '††††': 't',
 '‡‡‡‡': 'u',
 '§§§§': 'v',
 '¶¶¶¶': 'w',
 '####': 'x',
 '*****': 'y',
 '†††††': 'z'}

Upvotes: 3

Related Questions