0

Specs: Python 3.3.2

What I was trying to do:

Create a simple name and employee number dictionary
application. Have the user enter a list of names and employee numbers. Your
interface should allow a sorted output (sorted by name) that displays employee
names followed by their employee numbers.

What I came up with:

# enter a list of names of employees
# enter a list of employee numbers
# zip them together

def hrcat():
    name = input('Please enter names of employees: ')
    number = input('Please enter employee numbers: ')

    output = zip(name,number)
    print(output)

Question:

When given two lists of names and numbers, it returns the memory address of an object; something looks like this:

>>> hrcat()
Please enter names of employees: a,b,c
Please enter employee numbers: 1,2,3
<zip object at 0x7fea10ce4b90>

I wonder why it returns the memory address instead of the actual content of that object? I googled online but wasn't able to find answers addressing this question. Thank you for your insights!

1
  • Of course you googled online. Never saw anyone googling offline successfully... (SCNR) Commented Jun 26, 2013 at 7:26

1 Answer 1

2

In Python 3.x zip returns an iterator (which looks like <zip object at 0x7fea10ce4b90>). You can apply list to view the contents,

list(zip(name,number))

Although, if you are just making a dictionary, can forget about the list and use the iterator directly to populate it,

dict(zip(name,number))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Though it returns {'a': '1', 'b': '2', 'c': '3', ',': ','} when the input in my question above is given. Any thoughts on the comma entry?
@hakuna121 Just split(',') and optionally apply int.
@hakuna121 Your zip() call zips the strings a,b,c and 1,2,3, so that you match 'a' and '1', ',' and ',', 'b' and '2', ',' and ',' again, and 'c' and '3'. The duplicated (',', ',') tuple will be eaten up by the dict. This breaks as soon as you have numbers with more digits. So first, you should split(',') up the strings, making them lists, and only then zip() them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.