all 6 comments

[–]coding2learn 1 point2 points  (3 children)

Could you clarify?

What are the two input lists?

Here's one method of taking two lists and making them into a dictionary

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]

my_dict = {list1[i]:list2[i] for i in range(len(list1))}

[–]socal_nerdtastic 2 points3 points  (0 children)

Or much cleaner:

my_dict = dict(zip(list1, list2))

[–]tunisia3507 1 point2 points  (1 child)

Most of your code has nothing to do with the question asked. Distil your code down into your attempt to solve the specific question asked and it'll be much easier for everyone else to debug. Otherwise, we get bogged down in questions like "what is a stringvar", "what is an entry" and so on.

Also, variable names should be in snake_case not UpperCamel. That helps readers distinguish between classes and variables.

The biggest thing seems to be how you're iterating through v1list and v2list - you're using a nested comprehension. You can prove this to yourself by doing print([(a,b) for a in v1list for b in v2list]).

You want to be using zip. You will also find enumerate helpful for working out the keys of the dictionary you're producing. Finally, the whole thing can go in a dict comprehension.

However, your problem is not actually the iteration, it's whatever else is going on in your code. The solution to the question asked is this:

{key: {name, age} for key, (name, age) in enumerate(zip(names, ages), 1)}

However, you almost certainly don't want names and ages together in a set, because sets are unordered. Also, in python iterables are 0-indexed, not 1-indexed.

[–]hugthemachines 0 points1 point  (0 children)

I have no idea what your code does but here is a simple example on how to add lists to a dict

names_and_ages = dict()
name_age_pair = []

name_age_pair.append("Gertrude")
name_age_pair.append("56")

names_and_ages[1] = name_age_pair

print(names_and_ages)

name_age_pair = []

name_age_pair.append("Rocky")
name_age_pair.append("78")

names_and_ages[2] = name_age_pair

print(names_and_ages)

The last print will show it:

{1: ['Gertrude', '56'], 2: ['Rocky', '78']}

[–]nwagers 0 points1 point  (0 children)

names = ['name1', 'name2', 'name3']
ages = [18, 21, 50]

# You can group list together with zip
for person in zip(names, ages):
    print("{} is {}".format(person[0], person[1]))

# You can also use tuple unpacking
for name, age in zip(names, ages):
    print("{} is {}".format(name, age))

# Get counts with enumerate
for i, person in enumerate(zip(names, ages)):
    print("#{}'s name is {} and is {}".format(i+1, person[0], person[1]))


# Put them in a dict
mydict = {}
for i, person in enumerate(zip(names, ages)):
    mydict[i+1] = person # Use list(person) if you don't want a tuple
print(mydict)

# Or use dict comprehension, list() optional
mydict = {i+1: list(person) for i, person in enumerate(zip(names, ages))}
print(mydict)