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 7896257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:50:53+00:00 2026-06-03T07:50:53+00:00

I want to go from a data structure like: [[0, 12, 25, 45, 65,

  • 0

I want to go from a data structure like:

[[0,   12,  25,  45,  65,  100],
 [0,   0,   0,   255, 255, 255],
 [0,   0,   255, 255, 0,   0],
 [255, 255, 0,   0,   0,   0]]

to:

[[0,   12,  12, 25,   25,  45,  45,  65,  65, 100],
 [0,   0,   0,   0,   0,   255, 255, 255, 255, 255],
 [0,   0,   0,   255, 255, 255, 255, 0,   0,   0],
 [255, 255, 255, 0,   0,   0,   0,   0,   0,   0]]

(All columns except for the first and the last one are repeated).

I have the following list comprehension that works:

[[l[0]] + [x for sl in [[i, i] for i in l[1:-1]] for x in sl] + [l[-1]] for l in list_of_lists] 

but I was wondering if there is a more elegant and more readable way to write this.

  • 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-03T07:50:54+00:00Added an answer on June 3, 2026 at 7:50 am

    I was wondering if there is a more elegant and more readable way to write this.

    I think you should also consider that “more readable” doesn’t necessarily mean the same as “is it possible to cram this into one line?”. It can often mean more explicit and straight-forward iterative code. Perhaps you might find this function more readable:

    >>> def double_middle_items(l):
            if len(l) < 2:
                raise ValueError("there must be at least two items in l")
    
            result = [l[0]]
            for item in l[1:-1]:
                result.append(item)
                result.append(item)
            result.append(l[-1])
            return result
    
    >>> double_middle_items([1,2,3,4,5])
    [1, 2, 2, 3, 3, 4, 4, 5]
    

    Then your code becomes very simple:

    >>> [double_middle_items(l) for l in list_of_lists]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get data from two tables. I have patient first name and
I want to copy all data from one database to another which has the
I have a database i want to select data from data base of current
We want to replicate data from one database to several others (on another server).
i want use some data from a website with web service. i have a
I want to cache data from a json-webservice in my android app. The first
I have a data structure that looks like this: data =[ {'key_1': { 'calc1':
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I want a data structure that will allow querying how many items in last
I have a data-structure (in plist) that looks something like this: What i have

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.