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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:15:55+00:00 2026-05-29T21:15:55+00:00

i want to do a simple list manipulation in python: here is the way

  • 0

i want to do a simple list manipulation in python:
here is the way i did it using two for loops:

lst = []
coins = [1, 2, 5, 10, 20, 50, 100, 200]
maxi = 200
lst = lst + [0]*(maxi+5)
lst[0] = 1

for c in coins:
    for i in range(c, maxi+2):
        lst[i] += lst[i-c]

the above code gives me desired results, now i try to convert the two line loop into a one-liner

add = lambda i, c: lst[i] += lst[i-c]
[add(i, c) for i in range(c, maxi+1) for c in coins]

but i am getting an invalid syntax error, how can i pass the two variables c and i to lambda
and then manipulate the list at the two indexes.

Which is a more pythonic way of doing it?

Can anyone suggest shorter code?

I want to use only list comprehension. How can i implement the above code using list comprehension?

  • 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-05-29T21:15:57+00:00Added an answer on May 29, 2026 at 9:15 pm

    I think the double loop you have is perfectly fine — why do you want a shorter version?

    The error you get is because you put a statement inside a lambda function, where only expressions are allowed. You could write this as a list comprehension — something like (untested)

    [lst.__setitem__(i, lst[i] + lst[i - c]) 
     for c in coins for i in range(c, maxi+1)]
    

    but seriously — why should you? Apart from being obscure, it also creates a pointless long list of None values.

    The only thing I’d change about your code is the initialisation of lst, which is strangely spread out over three lines. The following line will be enough:

    lst = [1] + [0] * (maxi + 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using .NET 2.0 with standard controls. I want to display a simple list
I want a simple TextView to behave the way simple_list_item_1 in a ListView does.
I'm building a simple Todo List application where I want to be able to
I want to use List.Find() on a simple collection that does not implement Find().
I want to scrape a list of facts from simple website. Each one of
My problem is simple: I have a long list of elements that I want
Pretty simple. What is the difference among those three? I want to list every
I want to add simple list/combo box with action script 3 . but i
I am using Html.DropDownListFor to build a select list. It is a simple list
I want to display a simple list of languages. class Language extends Backbone.Model defaults:

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.