473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ 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 968
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_somethin g(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*******@hotm ail.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*******@hotm ail.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_operati on, 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
3081
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 has any advices?? #include <iostream> using namespace std;
10
15113
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
24
5743
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 macros */ #ifndef PUBLIC #define PUBLIC
4
3594
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; };   struct pcb *pcb_pointer;
3
2700
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> #include<stdlib.h> int main(void) {
0
1813
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 each node in the list must contain two pointers, one pointing forward and one pointing backwards. Each node in the list will contain 3 data values: an item ID (string), a quantity (integer) and a price (float). The ID will contain only letters and...
10
6565
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
8619
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
12
4019
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 the shared memory. I run fedora 9 and gcc 4.2. I am able to insert values in to the list, remove values from the list, but the problem is in traversing the list. Atlease one or 2 list nodes disappear when traversing from the base of the list or...
7
5760
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
7981
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2410
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 we have to send another system
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.