1

I would like the inner for loop to give me one value and goes on to the next iteration for each outer loop. Appreciate your responds.

Currently the result:

  • Personal NameJohn
  • Personal NamePeter
  • Personal Name123456
  • ID #John
  • ID #Peter
  • ID #123456
  • Emergency contactJohn
  • Emergency contactPeter
  • Emergency contact123456

Result should just be:

  • ID #123456
  • Personal NameJohn
  • Emergency contactPeter

    employees={'ID #','Personal Name','Emergency contact'}
    
    excel={'123456',
       'John',
       'Peter'}
    
    for key in employees:
        for value in excel:
           print(key + value) 
    
0

3 Answers 3

5

Use zip to iterate over two objects at the same time.

note: you are using sets (created using {"set", "values"}) here. Sets have no order, so you should use lists (created using ["list", "values"]) instead.

for key, value in zip(employees, excel):
    print(key, value)
Sign up to request clarification or add additional context in comments.

3 Comments

>>> type({'a','b'}) <class 'set'>
what I meant is that using {} creates a set instead of a list
val = {} creates and empty dictionary, not set. For an empty set, use val = set()
2

You can use zip after changing the type of your input data. Sets order their original content, thus producing incorrect pairings:

employees=['ID #','Personal Name','Emergency contact']
excel=['123456', 'John','Peter']
new_data = [a+b for a, b in zip(employees, excel)]

Output:

['ID #123456', 'Personal NameJohn', 'Emergency contactPeter']

Comments

0

First of all, use square brackets [] instead of curly brackets {}. Then you could use zip() (see other answers) or use something very basic like this:

for i in range(len(employees)):
    print(employees[i], excel[i])

1 Comment

Indeed, I've changed it (I'm not native english)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.