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

  • Home
  • SEARCH
  • 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 9012281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:57:20+00:00 2026-06-16T02:57:20+00:00

I am trying to create several new lists from one master list whereby the

  • 0

I am trying to create several new lists from one master list whereby the new lists contain similar items from the master list. Specifically, I have a list of bus routes. Here is a sample data set:

[u'Bus04_00_00_IB_pts_Line', u'Bus04_00_00_OB_pts_Line', u'Bus15_00_00_IB_pts_Line', u'Bus15_00_00_OB_pts_Line']

Most bus routes have an inbound (IB) and an outbound (OB) item, (and some have multiple IBs and OBs, and some have only one route, b/c they are loop routes). Eventually, I want to merge the IB and OB routes in mapping software (which I already know how to do)…

I originally created the filenames so that the first 5 characters represent the bus route, whether or not it’s IB or OB. Therefore, I am able to group similar items based on the first 5 characters. For example, when I write:

for route in routes:
    print route[0:5]

I get:

>>> 
Bus04
Bus04
Bus15
Bus15

How can I “group” the files that pertain to Bus04 and Bus04, and Bus15 and Bus15 into new lists, such that I get:

[u'Bus04_00_00_IB_pts_Line', u'Bus04_00_00_OB_pts_Line'] and [u'Bus15_00_00_IB_pts_Line', u'Bus15_00_00_OB_pts_Line'] as separate lists?

I am thinking something along the lines of looping through each item, looking at the first five characters of each, then either create a new list with each new five character item that comes up (and add that item to the new list) or checking whether a list already exists and appending the similar item to it.

I’m having a hard time writing this out in code, so any help is greatly appreciated!

  • 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-16T02:57:21+00:00Added an answer on June 16, 2026 at 2:57 am

    I would use collections.defaultdict for this:

    import collections
    
    L = [u'Bus04_00_00_IB_pts_Line', u'Bus04_00_00_OB_pts_Line', u'Bus15_00_00_IB_pts_Line', u'Bus15_00_00_OB_pts_Line']
    d = collections.defaultdict(list)
    for elem in L:
        d[elem.split('_')[0]].append(elem)
    print(dict(d))
    

    This produces:

    {u'Bus04': [u'Bus04_00_00_IB_pts_Line', u'Bus04_00_00_OB_pts_Line'],
     u'Bus15': [u'Bus15_00_00_IB_pts_Line', u'Bus15_00_00_OB_pts_Line']}
    

    Unlike some of the other solutions proposed thus far, this works irrespective of the order in which entries appear in the input list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a master list of several excel data that I
Hi i'm trying to create a league table in which i have several different
I'm trying to insert several newly created items to the database. I have a
Currently i have an application that reads and writes several properties from one or
I'm new to Java and JavaFX I have php,javascript,HTML experience... I'm trying to create
I have a list of objects (created from several text files) in C#.net that
I'm trying to create an image containing text. I've tried several examples I've found
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I have a long scrolling list of EditText items created by a SimpleCursorAdapter and
I have several ListActivties set up in my Android Application. What I am trying

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.