Urgent Help Needed- TypeError: map() missing 1 required positional argument: 'seq'

Screen Link:
https://app.dataquest.io/m/263/functional-programming/5/the-map-function

My Code:

lines = read('example_log.txt')
ip_addresses = list(map(lambda x: x.split(' ')[0], lines))
ip_addresses = list(map(lambda x: x.split()[0], lines))
print(ip_addresses)



#Pseudocode for map.
def map(func, seq):
    # Return `Map` object with
    # the function applied to every
    # element.
    return map(
        func(x)
        for x in seq
   )

values = [1, 2, 3, 4, 5]
add_10 = list(map(lambda x: x + 10, values))

What I expected to happen:
I expected to get the output below

[11, 12, 13, 14, 15]

What actually happened:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-472f23e7a8a1> in <module>
     21 #print(ip_addresses)
     22 values = [1, 2, 3, 4, 5]
---> 23 add_10 = list(map(lambda x: x + 10, values))

<ipython-input-1-472f23e7a8a1> in map(func, seq)
     14     # the function applied to every
     15     # element.
---> 16     return map(
     17         func(x)
     18         for x in seq

TypeError: map() missing 1 required positional argument: 'seq'

I am not sure why this code is not working as this was the code shown in the examples and the answer provided by dataquest in the “Building a Data Pipeline” course. I can not progress in the course If I dont solve this error. Any Help is Greatly appreciated

image

PythonError

Hi @jacobhassan03 and welcome to the community!

Although I haven’t done this mission yet, I thought I would try to help you out as I do have some experience working with the map() function.

So, the first thing I did was copy and paste your code to reproduce the error. I successfully did that; I got the same error you did, Jacob. Then I looked at the text from the link you provided and noticed that the returned map object was actually capitalized:

def map(func, seq):
    # Return `Map` object with
    # the function applied to every
    # element.
    return Map(
        func(x)
        for x in seq
    )

So I changed your code so that the returned map object was capitalized: Map (because python is case sensitive.) This explains why you are getting the error you’re getting: you are trying to return the function map which you are currently defining which takes in two arguments: func and seq. But the line that says return map only has one argument (func) but does not have an argument for seq.

It’s at this point I actually read the inline comments for the map function we are playing around with here:

:man_facepalming: This isn’t “actual code”…it’s pseudocode meant to show us the concept of what map does!

However, since I ran this code several times (as I’m sure you have as well…) I essentially overwrote the code for the actual map function! So at this point, I keep getting the following error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-bf3b856d9163> in <module>
     12    )
     13 values = [1, 2, 3, 4, 5]
---> 14 add_10 = list(map(lambda x: x + 10, values))

<ipython-input-1-bf3b856d9163> in map(func, seq)
      7     # the function applied to every
      8     # element.
----> 9     return Map(
     10         func(x)
     11         for x in seq

NameError: name 'Map' is not defined

Which makes sense because Map is (as the error message above states) not defined!

So I restored the code using the circular arrows at the bottom of the screen to Restore Initial Code and tried to copy/paste the solution but I still keep getting the error that Map is not defined! I logged out of DQ, signed back in…still getting the same error. And then all of a sudden, as I was about to call in some @Sahil backup, it miraculously stopped generating the error!

Think I could just leave it alone at this point? NOPE!!! I had to try to figure out what was happening here…and now, once again, I’m trapped in this limbo where the built-in map function has been overwritten by the above pseudocode and tells me that Map is not defined even though I have restored the code and I’m just inputting the solution code (ie I am not redefining the map function anymore).

Moral of the story: don’t write a def for built-in functions! :laughing: I’m hoping that it will eventually sort itself out as it did before.

I’m curious what you get @jacobhassan03 if you were to restore your code and then try running:

values = [1, 2, 3, 4, 5]
add_10 = list(map(lambda x: x + 10, values))

because if you haven’t overwritten the built-in map function, this code should work!

UPDATE: I am finally out of the loop again! I restored code, logged out, and then rebooted my terrible windows machine and by the time I got back to the desktop and logged into DQ again, no more errors! I tried running:

values = [1, 2, 3, 4, 5]
add_10 = list(map(lambda x: x + 10, values))

and got the expected output without errors! YAY!

Thanks for going above and beyond Mike! Much appreciated :smile:

No worries, it was my pleasure! :sunglasses:

Did you manage to get rid of all the errors? Can you use the built-in map() function okay?

Hi Mike,

I was able to use the built-in map(), but now the built-in filter() is giving me issues.

see the code below

lines = read(‘example_log.txt’)
ip_addresses = list(map(lambda x: x.split()[0], lines))
filtered_ips = list(filter(lambda x: int(x.split(’.’)[0]) <= 20, ip_addresses))
print(filtered_ips)

See the error output below


NameError Traceback (most recent call last)
in
7 lines = read(‘example_log.txt’)
8 ip_addresses = list(map(lambda x: x.split()[0], lines))
----> 9 filtered_ips = list(filter(lambda x: int(x.split(’.’)[0]) <= 20, ip_addresses))
10 print(filtered_ips)

in filter(evaluate, seq)
9 # the evaluate function applied to every
10 # element.
—> 11 return Map(
12 x for x in seq
13 if evaluate(x) is True

NameError: name ‘Map’ is not defined

Did you copy/paste the pseudocode for the filter() function like we did to the map() function? If so, that’s the problem! That pseudocode is not actual code and you should not copy/paste it into the console. If you do, you overwrite the built in filter() function and it causes the above error. Try doing what I did to get out of this loop: restore the code, logout, reboot and try again!