473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 825
On May 9, 11:04*am, dmitrey <dmitrey.kros...@scipy.orgwrote:
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...@gmail.comwrote:
On May 9, 11:04am, dmitrey <dmitrey.kros...@scipy.orgwrote:
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.orgwrote:
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...@gmail.comwrote:
On May 9, 11:04am, dmitrey <dmitrey.kros...@scipy.orgwrote:
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.orgwrote:
On 9 , 13:17, Paul Hankin <paul.han...@gmail.comwrote:
>On May 9, 11:04am, dmitrey <dmitrey.kros...@scipy.orgwrote:
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...@invalid.invalidwrote:
dmitrey <dmitrey.kros...@scipy.orgwrote:
On 9 ôÒÁ, 13:17, Paul Hankin <paul.han...@gmail.comwrote:
On May 9, 11:04šam, dmitrey <dmitrey.kros...@scipy.orgwrote:
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 #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...@gmail.comwrote:
On May 9, 11:04�am, dmitrey <dmitrey.kros...@scipy.orgwrote:
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.__getitem__, list2)

--
Arnaud
Jun 27 '08 #8

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

Similar topics

5
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....
20
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:...
1
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...
38
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 =...
17
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...
27
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....
73
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...
30
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...
1
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:...
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...

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.