Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Sequence Assignment to Words

Improve Article
Save Article
  • Last Updated : 22 Nov, 2022
Improve Article
Save Article

Given a String of words, assign an index to each word.

Input : test_str = ‘geeksforgeeks is best’ 

Output : {0: ‘geeksforgeeks’, 1: ‘is’, 2: ‘best’} 

Explanation : Index assigned to each word. 

Input : test_str = ‘geeksforgeeks best’ 

Output : {0: ‘geeksforgeeks’, 1: ‘best’} 

Explanation : Index assigned to each word.

Method #1 : Using enumerate() + dict() + split()

In this, we first perform task of split() and then add index component to map each word with index using enumerate().

Python3




# Python3 code to demonstrate working of
# Sequence Assignment to Words
# Using split() + enumerate() + dict()
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using dict() to convert result in idx:word manner
res = dict(enumerate(test_str.split()))
     
# printing result
print("The Assigned Sequence : " + str(res))

Output

The original string is : geekforgeeks is best for geeks
The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

Method #2 : Using zip() + count() + dict()

In this, the count() component renders the index logic and pairing of each word to index is done using zip().

Python3




# Python3 code to demonstrate working of
# Sequence Assignment to Words
# Using zip() + count() + dict()
from itertools import count
 
# initializing string
test_str = 'geeksforgeeks is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using dict() to convert result in idx:word manner
# count() from itertools used for this task
res = dict(zip(count(), test_str.split()))
     
# printing result
print("The Assigned Sequence : " + str(res))

Output

The original string is : geekforgeeks is best for geeks
The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!