473,480 Members | 4,939 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I not make a list?

It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
I hope I've described this adequately.
Thank you...

Nov 29 '07 #1
3 961
Just Another Victim of the Ambient Morality schrieb:
It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
You want a generator expression. Or a generator.
res = (apply_something(e) for e in my_iterable)
or

def g(mit):
for e in mit:
yield apply_something(e)
Both only get evaluated step by step during the iteration, reducing
memory consumption.

Diez
Nov 29 '07 #2
On 11/29/07, Just Another Victim of the Ambient Morality
<ih*******@hotmail.comwrote:
It may sound like a strange question but that's probably only because I
don't know the proper terminology. I have an iterable object, like a list,
and I want to perform a transform on it (do an operation on each of the
elements) and then pass it onto something else that expects and iterable.
I'm pretty sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Now, I could just make a list, using a list comprehension, performing my
operation on each element, and then pass that list on, knowing that it is
iterable. However, I was wondering if there was a way I can do virtually
this without having to actually allocate the memory for a list. Creating a
stock iterator or generator or whatever it's called, with a passed in
operation?
Well I think what you want is to use "()" instead of "[]"
>>l = (i for i in range(1,20))
l
<generator object at 0xb7f482ac>

Cheers,
--
Amit Khemka
Nov 29 '07 #3
On 2007-11-29, Just Another Victim of the Ambient Morality
<ih*******@hotmail.comwrote:
It may sound like a strange question but that's probably only
because I don't know the proper terminology. I have an
iterable object, like a list, and I want to perform a transform
on it (do an operation on each of the elements) and then pass
it onto something else that expects and iterable. I'm pretty
sure this something else doesn't need a list, either, and just
wants to iterate over elements.
Try itertools.imap.

something_else(imap(do_operation, an_iterable))

--
Neil Cerutti
You've got to take the sour with the bitter. --Samuel Goldwyn
Nov 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3065
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
15095
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
5713
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
3578
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
2689
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
1799
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
6551
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
8601
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
3985
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
5753
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.