473,584 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple question about Python list

hi all,
suppose I have 2 lists
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements

howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.

list3 = ['elem0', 'elem2', 'elem4']?

Thank you in advance, D.
Jun 27 '08 #1
7 833
On May 9, 11:04*am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements

howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.
list3 =[list1[i] for i in list2]

--
Paul Hankin
Jun 27 '08 #2
Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
(preferable) 2.3 compatible.
Are there other solutions?
D.

On 9 , 13:17, Paul Hankin <paul.han...@gm ail.comwrote:
On May 9, 11:04am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements
howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.

list3 =[list1[i] for i in list2]

--
Paul Hankin
Jun 27 '08 #3
On May 9, 6:25 am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
(preferable) 2.3 compatible.
Are there other solutions?
D.

On 9 , 13:17, Paul Hankin <paul.han...@gm ail.comwrote:
On May 9, 11:04am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements
howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.
list3 =[list1[i] for i in list2]
--
Paul Hankin
Why don't you try Paul's suggestion under Python 2.3 and see what
happens?
Jun 27 '08 #4
dmitrey <dm************ *@scipy.orgwrot e:
On 9 , 13:17, Paul Hankin <paul.han...@gm ail.comwrote:
>On May 9, 11:04am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements
howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.

list3 =[list1[i] for i in list2]

Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
(preferable) 2.3 compatible.
Are there other solutions?
D.
Did you try Paul's suggestion?

Python 2.3.5 (#1, Oct 13 2005, 09:17:23)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
list2 = [0, 2, 4]
list3 =[list1[i] for i in list2]
list3
['elem0', 'elem2', 'elem4']

Jun 27 '08 #5
Hmm... I thought this issue is available from Python2.5 only. I have
no other interpreters in my recently installed KUBUNTU 8.04.

Thanks all,
D.

On 9 Тра, 13:41, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
On 9 ôÒÁ, 13:17, Paul Hankin <paul.han...@gm ail.comwrote:
On May 9, 11:04šam, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements
howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.
list3 =[list1[i] for i in list2]
Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
(preferable) 2.3 compatible.
Are there other solutions?
D.

Did you try Paul's suggestion?

Python 2.3.5 (#1, Oct 13 2005, 09:17:23)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.>>l ist1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
>list2 = [0, 2, 4]
list3 =[list1[i] for i in list2]
list3

['elem0', 'elem2', 'elem4']
Jun 27 '08 #6
dmitrey wrote:
Hmm... I thought this issue is available from Python2.5 only. I
have no other interpreters in my recently installed KUBUNTU 8.04.
"aptitude install python2.4" helps.

Regards,
Bjrn

--
BOFH excuse #10:

hardware stress fractures

Jun 27 '08 #7


dmitrey wrote:
Ok, I use Python 2.5 but I try my code to remain Python 2.4 and
(preferable) 2.3 compatible.
Are there other solutions?
D.

On 9 ���, 13:17, Paul Hankin <paul.han...@gm ail.comwrote:
On May 9, 11:04�am, dmitrey <dmitrey.kros.. .@scipy.orgwrot e:
list1 = ['elem0', 'elem1', 'elem2', 'elem3', 'elem4', 'elem5']
and
list2 = [0, 2, 4] # integer elements
howto (I mean most simple recipe, of course) form list3 that contains
elements from list1 with indexes from list2, i.e.
list3 =[list1[i] for i in list2]

--
Paul Hankin
This works under python 2.0+, I think.

Before that, you could have written:

list3 = map(list1.__get item__, list2)

--
Arnaud
Jun 27 '08 #8

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

Similar topics

5
2931
by: Vio | last post by:
I need to test for equality between simple 2 decimal numbers. For example: if (10 + 15.99) == 25.99: do some stuff... The preceding sentence should be TRUE, but to Python it appears FALSE. Which is wrong. Perhaps because Python translates "25.99" to "25.98999999999999998" and not "25.99", which may be the reason for this error (me...
20
5173
by: Matthew Thorley | last post by:
My friend sent me an email asking this: > I'm attemtping to decide which scripting language I should master and > was wondering if it's possible to do > these unixy awkish commands in python: > > How to find the amount of disk space a user is taking up: > > find / -user rprice -fstype nfs ! -name /dev/\* -ls | awk '{sum+=$7};\ > {print...
1
1776
by: Simon Roses Femerling | last post by:
Thx for your response sqllite looks very interesting :) Here is the python module: http://sourceforge.net/projects/pysqlite/ I'm familiar with mysql and postgresql but there are too heavy and I dont want to install a database. I'm looking for something like sqllite thx
38
3507
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
17
7032
by: Igorati | last post by:
Hello all, I am still needing some help on this code, I have gone a bit further on it. Thank you for the help. I am trying to understand how to make the file searchable and how I am to make the deposit and withdrawl interact with the transaction class. I need to just search the file only for the deposits and withdrawls and the amount in the...
27
2364
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here. It is available at: http://staff.washington.edu/sabbey/py_do Good background on thunks can be found in ref. . Simple Thunks
73
4577
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix...
30
1801
by: Brian Elmegaard | last post by:
Hi, I am struggling to understand how to really appreciate object orientation. I guess these are FAQ's but I have not been able to find the answers. Maybe my problem is that my style and understanding are influenced by matlab and fortran. I tried with the simple example below and ran into several questions: 1: Why can't I do: def...
1
2211
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner: http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/ also pointed to a simple eval function by Fredrik Lundh:...
7
1189
by: bvdp | last post by:
Is there a simple/safe expression evaluator I can use in a python program. I just want to pass along a string in the form "1 + 44 / 3" or perhaps "1 + (-4.3*5)" and get a numeric result. I can do this with eval() but I really don't want to subject my users to the problems with that method. In this use I don't need python to worry about...
0
7897
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7829
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...
0
8330
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...
1
7940
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8198
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...
0
3824
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...
0
3850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2336
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
1162
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...

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.