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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:34:21+00:00 2026-06-18T05:34:21+00:00

I am trying to create some lists from other lists putting some conditions along

  • 0

I am trying to create some lists from other lists putting some conditions along the way. I want to write these lists finally into a csv. Here is the code which I attempted.

x = [None]*1000
y = [None]*1000
z = [None]*1000
i = 0 
for d in range(0,len(productID)):
    for j in range(0,len(productID[d])):
        if productID[d][j].startswith(u'sku'):

            x[i] = map[productID[d][j]]
            y[i] = name[d]
            z[i] = priceID[d][productID[d][j]].get(u'e')

            i = i + 1  

plan_name = x[0:i]
dev_name  = y[0:i]
dev_price = z[0:i]

This is working fine, but I assume there should be a better way of doing this. Can anyone suggest how can I create a list while looping without having to define it first?

  • 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-18T05:34:22+00:00Added an answer on June 18, 2026 at 5:34 am

    You use .append() to add items to an initially empty list instead.

    x, y, z = [], [], []
    for d, sublist in enumerate(productID):
        for entry in sublist:
            if entry.startswith(u'sku'):
                x.append(map[entry])
                y.append(name[d])
                z.append(priceID[d][entry].get(u'e'))
    

    Note that python can loop over sequences directly, and you usually do not need indexes at all. I’ve used the enumerate() function to add indexes to the outer loop instead (because you seem to need to index into name and priceID there).

    .append() adds items to the end of the list:

    >>> foo = []
    >>> foo.append('bar')
    >>> foo.append('spam')
    >>> foo
    ['bar', 'spam']
    

    You may want to read over the Python tutorial; it explains how python lists work nicely.

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

Sidebar

Related Questions

I'm trying to create a list of tasks that I've read from some text
I am trying to create some cucumber features with references to other objects. I
I'm trying to create a standalone HTML5 document that utilizes SQlite or some other
I'm trying to create a list that some of the elements are defined and
I am trying to create an ordered list on IE7 but for some reason
I am trying to create some capacity planning reports and one of the requrements
So I'm trying to create some graphs using Core Plot, but for the following
I am trying to create some reusable components in Silverlight2 . The difficulty comes
I am trying to create some charts of data (eg http://www.amibroker.com/ ). Is there
I am trying to create some rules and facts on a certain situation. The

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.