Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:18:48+00:00 2026-06-09T20:18:48+00:00

I’m new to python so apologies for the easy questions. I’m certainly missing something

  • 0

I’m new to python so apologies for the easy questions. I’m certainly missing something which is getting me confused.

This has something to do with nesting, splitting, and I’m guessing for loops but it’s not working out for me.

So here’s my original string:

name_scr = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'

I’m trying to create a data structure – dict with the names and scores within it.

So this is what I started with:

    test_scr = { } 
    new_name_scr = [list.split('-') for list in name_scr.split('|')]
# [['alvinone', '90,80,70,50'], ['simonthree', '99,80,70,90'], ['theotwo', '90,90,90,65']]

# this is not  right, and since the output is a list of lists, I cannot split it again 

I get stuck splitting this for the third time at the comma. So then I try the following:

test_scores = {}
for student_string in name_scr.split('|'):
  for student in student_string.split('-'):
    for scores in student.split(','):
      test_scores[student] = [scores]

#but my result for test_scores (below) is wrong
#{'alvinone': ['alvinone'], '99,80,70,90': ['90'], 'theotwo': ['theotwo'], '90,80,70,50': ['50'], '90,90,90,65': ['65'], 'simonthree': ['simonthree']}

I want it to look like this:

  {'alvinone': [99 80 70 50], 'simonthree': [90 80 70 90], 'theotwo': [90 90 90 65]} 

So that when I do this:

print test_scores['simonthree'][2] #it's 70

Please help me here. Keep in mind I’m brand new to python so I don’t know too much just yet. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:18:49+00:00Added an answer on June 9, 2026 at 8:18 pm

    Your split was correct, all you need to do is iterate over the values and convert it to dict, and to easily access the elements of the dict key, your value should be a list not a string…

    Note: while splitting you have used variable name as ‘list’, which is not a good idea.

    check this…

    In [2]: str = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'
    
    In [3]: str_list = [l.split('-') for l in str.split('|')]
    
    In [4]: str_list
    Out[4]:
    [['alvinone', '90,80,70,50'],
     ['simonthree', '99,80,70,90'],
     ['theotwo', '90,90,90,65']]
    
    In [5]: elem_dict = {}
    
    In [6]: for elem in str_list:
       ...:     elem_dict[elem[0]] = [x for x in elem[1].split(',')]
       ...:
    
    In [7]: print elem_dict
    {'simonthree': ['99', '80', '70', '90'], 'theotwo': ['90', '90', '90', '65'], 'alvinone': ['90
    ', '70', '50']}
    
    In [8]: elem_dict['simonthree'][2]
    Out[8]: '70'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.