Python Forum
Converting '1a2b3c' string to Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting '1a2b3c' string to Dictionary
#5
from more_itertools import chunked, sliced

spam = '1a2b3c4d'

# without using more_itertools 
print(dict(zip(spam[::2], spam[1::2])))

# using more_itertools
print(dict(chunked(spam, 2)))
print(dict(sliced(spam, 2, strict=True)))
Output:
{'1': 'a', '2': 'b', '3': 'c', '4': 'd'} {'1': 'a', '2': 'b', '3': 'c', '4': 'd'} {'1': 'a', '2': 'b', '3': 'c', '4': 'd'}
menator01 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Converting '1a2b3c' string to Dictionary - by buran - May-13-2022, 05:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help converting string to int dedesssse 7 2,694 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  Beautify dictionary without converting to string. sharoon 6 3,388 Apr-11-2021, 08:32 AM
Last Post: buran
  how to deal with problem of converting string to int usthbstar 1 1,981 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Converting data in CSV and TXT to dictionary kam_uk 3 1,998 Dec-22-2020, 08:43 PM
Last Post: bowlofred
  Converting string to hex triplet menator01 4 4,341 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  extract a dictionary from a string berc 4 2,872 Jul-30-2020, 06:58 AM
Last Post: berc
  simple f-string expressions to access a dictionary Skaperen 0 1,531 Jul-15-2020, 05:04 AM
Last Post: Skaperen
  converting string object inside a list into an intiger bwdu 4 2,626 Mar-31-2020, 10:36 AM
Last Post: buran
  problem coverting string data file to dictionary AKNL 22 6,470 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Converting query string as a condition for filter data. shah_entrance 1 1,802 Jan-14-2020, 09:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020