NewToPython43532 NewToPython43532 - 1 year ago 85
Python Question

How to pair items in two different lists using range?

Im trying to make a dictionary using the two lists, Loop through a sorted version of that dictionary. Print out the name, followed by a space, followed by a colon, followed by a space, then finally the alias.
Eg. Stirling : Duchess
put what I did literally just prints out the name dict. ??

name = ["Stirling","Lana","Cyril","Pam","Ray","Cheryl"]
alias =["Duchess","Truckasaurus","Chet","Cookie Monster","Gilles de Rais","Cherlene"]

randomV = {}

for i in range(0,len(name)):
item = [name, alias]

for word in name and alias:

Answer Source

I think you want

names = ["Stirling","Lana","Cyril","Pam","Ray","Cheryl"]
aliases =["Duchess","Truckasaurus","Chet","Cookie Monster","Gilles de Rais","Cherlene"]

for name, alias in zip(names, aliases):
    print "{} : {}".format(name, alias)

To create a single dict, d = dict(zip(names, aliases)).

Your attempt has several problems: item is the same exact value each time through your first loop, since you never actually use the value of i, and name and alias simply evaluates to the first non-empty list (i.e., name). You wanted perhaps

items = []
for i in range(len(name)):
    item[i] = (name[i], alias[i])

for word in items:
    print(word[0], ":", word[1])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download