Getting an error: "ValueError: dictionary update sequence element #0 has length 1; 2 is required" when used map function

Adarsh Namdev
characters = [num for num in range(20, 41)]
mapper = list(map(chr, characters))

print (mapper)      # returning the list of all the ASCII characters w.r.t its value.

mapper = dict(map(chr, characters)) 
print(mapper)

In the above code, when I print 'mapper' converted to list object. I am getting the correct output. But when the same is converted to dictionary object. I am getting an error...

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    mapper = dict(map(chr, characters))
ValueError: dictionary update sequence element #0 has length 1; 2 is required

So, does that mean that map function can only covert into object type- tuple or list and NOT dictionary?

The result I am expecting is where ASCII value to be the dictionary 'keys' and the respective ASCII character as dictionary 'values' something like this:

{20: "\x14", 21: "\x15", 22: "\x16", ............ , 39: " ' ", 40: " ( "}
abccd

It seems like you really want to use map, you can, but as @e4c5 stated: "that would be inelegant, more lines of code, unpythonic and slower", which is entirely true. But I'll provide an example using map for you, since that's what you seemed to want:

The reason your code doesn't work is because a dictionary needs a key and a value, right now, you only the value, no keys. You can use zip to zip the key from characters to the value from map:

characters = [num for num in range(20, 41)]    
mapper = dict(zip(characters,map(chr, characters)))
print(mapper)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert To Dictionary in Robot Framework gives the error "ValueError: dictionary update sequence element #0 has length 1; 2 is required"

default_attrs.update(attrs) ValueError: dictionary update sequence element #0 has length 1; 2 is required

ValueError: dictionary update sequence element #0 has length 1; 2 is required

Python: getting type error when import function used

getting error map function is not defined

When a map (used as a function) is given two arguments

Get rid of eslint@typescript-eslint/no-unused-vars error when index is not used in map function typescript

getting error this.state.datas.map is not a function

Getting a map and collectors error in kotlin function

Getting error as 'TypeError: results.map is not a function'

Keep getting an error: listDevices.map is not a function

Getting error this.httpClient.get(...).map is not a function when running ng test

I am not able to add user input to state properly, getting map not a function error when it is

Getting cannot read properties of undefined error when trying to map through prop generated by getStaticProps function

The use of map when the function that map is used on has arrays of inputs

Haskell: Understanding the map function when used with lambda function

Haskell: Understanding function application operator when used with map function

Variable undeclared error when used inside a function

code works when it's not in a function, error when used in a function

function is async but getting error that await keyword cannot be used

Getting error when trying to subscribe to function in ngOnInit

getting ".then() is not a function" error when using AngularJS

Getting Error when adding an async function on NodeJS

Getting a MYSQL 1064 error when creating a function

Getting error when declare function in javascript

Getting an error when try to execute java function

getting the error when using tableview function

Getting Error when Wrong $_GET Parameter in URL is Used

Getting a "cannot convert" error when trying to used a cached token

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive