• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Split String into Dictionary

Using Python to Split String into Dictionary

June 2, 2022 Leave a Comment

To split a string into a dictionary using Python, the easiest way is to use the Python split() function and dictionary comprehension. With this method, you will get a dictionary where the keys are numbers and the values are the words of the string.

string = "this is a string with some words"

d = { idx: ele for idx, ele in enumerate(string.split())}

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

You can also use a loop explicitly to create a dictionary from a string.

string = "this is a string with some words"

d = dict()

for idx, ele in enumerate(string.split()):
    d[idx] = ele

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

When working with strings and text in Python, the ability to manipulate and create new objects from strings can be useful.

One such situation is if you want to take a string and create a dictionary with the words from the string. You can split a string into a dictionary easily with the Python split() function and dictionary comprehension.

Given a string, you can use split() to split a string by a delimiter into a list.

Then, you can use enumerate() to get the index and values of the list of words and use dictionary comprehension to create a new dictionary.

Below is an example of how you can split a string into a dictionary using Python.

string = "this is a string with some words"

d = { idx: ele for idx, ele in enumerate(string.split())}

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

How to Split String into Dictionary of Words Using Python

Above is a one line solution to the question of how to split a string into a dictionary of words in your Python code.

You can be more explicit and use a for loop if there are any other manipulations or additive things you need to add to your dictionary.

Below is another example of how to split a string into a dictionary.

Notice we are still using split() and enumerate(). The difference now is that we are building the dictionary from scratch.

string = "this is a string with some words"

d = dict()

for idx, ele in enumerate(string.split()):
    d[idx] = ele

print(d)

#Output:
{0: 'this', 1: 'is', 2: 'a', 3: 'string', 4: 'with', 5: 'some', 6: 'words'}

Hopefully this article has been useful for you to learn how to split a string into a dictionary using Python.

Other Articles You'll Also Like:

  • 1.  How to Group and Aggregate By Multiple Columns in Pandas
  • 2.  Print Object Attributes in Python using dir() Function
  • 3.  Get All Substrings of a String in Python
  • 4.  Generate Random Float Between 0 and 1 Using Python
  • 5.  Using Python to Add Trailing Zeros to String
  • 6.  Convert List into Tuple Using Python
  • 7.  pandas nsmallest – Find Smallest Values in Series or Dataframe
  • 8.  Using Python to Read Random Line from File
  • 9.  Using Python to Count Items in List Matching Criteria
  • 10.  Find Magnitude of Complex Number in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy