-5

I have a list with data that can be split into JSON like objects/strings. I have to convert this list into a dictionary based on that. My list:

['1: geek', '2: geek', '3: geek', '4: ', '5: ', '6: ']

I want a dictionary like this:

{'1': 'geek', '2': 'geek', '3': 'geek', '4': '', '5': '', '6': ''}

All of this must be done using Python.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54

2 Answers2

2

Iterate over the list, splitting the strings by the colon. Because there are spaces with this, you'll want to use strip() on the strings as you add the values to your dictionary.

my_list = ['1: geek', '2: geek', '3: geek', '4: ', '5: ', '6: ']

new_dict = {}
for item in my_list:
    key = item.split(':')[0].strip()
    val = item.split(':')[1].strip()
    new_dict[key] = val

print(new_dict)
Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
  • 3
    Apparently that's not the output OP wants – DeepSpace Jul 11 '19 at 11:02
  • 1
    I think this is what OP wants, and that the language barrier has made this difficult for him or her to convey to us. – Joshua Schlichting Jul 11 '19 at 11:18
  • What "language barrier"? OP used Python code to show the output. Unless you referred to Python (and not English)... – DeepSpace Jul 11 '19 at 11:20
  • "like this i am having I want" I think Josh is referring to the obvious. – Austin Heller Jul 11 '19 at 11:25
  • 2
    I feel that Mohd Abdul Raoof, from Hyderabad, Telangana, India, uses English as a second language, based on the initial text they posted. With that in mind, I have attempted to read past that to help the user with their actual issue, instead of shutting it down. I'm 99% certain this is what they're looking for. – Joshua Schlichting Jul 11 '19 at 11:28
  • Again. OP showed a **Python** example of the output they want. The language they speak/write is irrelevant – DeepSpace Jul 11 '19 at 11:31
  • 1
    This is true. The desired output would not have worked as an actual dictionary, and would "break" in Python. However, based on the text and totality of the post, and given the users low reputation, I believe the user is a beginner to Python (and probably programming as a whole), and that they still want a dictionary. This does enter the realm of "assumptions," but in this case, I think it is to the benefit of the user asking the question. I would not apply this to all cases where a user has incorrect syntax. – Joshua Schlichting Jul 11 '19 at 11:34
  • OP does not have an incorrect syntax. The first snippet shows a list of strings and the second one shows a set of strings. – DeepSpace Jul 11 '19 at 11:36
  • This is true. However, based on the user's tags `python`, `list`, and `dictionary`, and the initial question text of "I have to convert into dictionary...", I made the assumption that they wanted their list converted to dictionary, and not a set. This assumption was also made when considering the presence of colons `:` in each of the list items. I felt it was obvious what the user was trying to do, but they just weren't able to articulate it properly. – Joshua Schlichting Jul 11 '19 at 11:40
  • @DeepSpace, at the end of the day, you are technically in the right. This was poorly written when it came to the details. On that note, I'd like to point out that the Question/Title of the question here is ***"How to convert a list into Dictionary using Python?"*** – Joshua Schlichting Jul 11 '19 at 11:51
0

Assuming that your code sample is meant to create a dict with the numbers being the key of the dict, in this case you could try this adapted from here:

mylist = ['1: geek', '2: geek', '3: geek', '4: geek', '5: geek', '6: geek']
mydict = dict((b.split(':')+[1])[:2] for b in mylist)
mydict

This outputs:

{'1': ' geek',
 '2': ' geek',
 '3': ' geek',
 '4': ' geek',
 '5': ' geek',
 '6': ' geek'}

# Access a single item
mydict.get('1')
>>> ' geek'
Moritz
  • 95
  • 1
  • 11