473,408 Members | 1,826 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,408 software developers and data experts.

list example

Hi,
I wrote this small example to illustrate the usage of lists:

even=[]
odd=[]

for i in range(1,10):
if i%2:
odd.append(i)
else:
even.append(i)

print "odd: "+str(odd)
print "even: "+str(even)

numbers=even
numbers.extend(odd)
print "numbers:"+str(numbers)
numbers.sort()
print "sorted numbers:"+str(numbers)
any comment, suggestion? Is there something not elegant?

Thnx
PAolo

Apr 22 '06 #1
5 1164
No substantive problems. The str() calls are unnecessary, print calls
list.__str__ already. You can replace the loop with list comprehensions or
slices. Same result, a bit more succinct. See these pages for more:

http://docs.python.org/lib/typesseq.html
http://docs.python.org/tut/node7.html (section 5.1.4)
PAolo wrote:
Hi,
I wrote this small example to illustrate the usage of lists:

even=[]
odd=[]

for i in range(1,10):
if i%2:
odd.append(i)
else:
even.append(i)

print "odd: "+str(odd)
print "even: "+str(even)

numbers=even
numbers.extend(odd)
print "numbers:"+str(numbers)
numbers.sort()
print "sorted numbers:"+str(numbers)
any comment, suggestion? Is there something not elegant?

Thnx
PAolo

Apr 22 '06 #2
PAolo wrote:
any comment, suggestion? Is there something not elegant?


Try this:

even = range(10)[0::2]
odd = range(10)[1::2]

Apr 22 '06 #3
PAolo>any comment, suggestion? Is there something not elegant?<

Here you can see many little differences, some of them reflect just my
style, but some other of them are Python standard style guidelines:

even = []
odd = []

for i in xrange(1, 10):
if i % 2:
odd.append(i)
else:
even.append(i)

print "Odd:", odd
print "Even:", even

numbers = even + odd
print "Numbers:", numbers
numbers.sort()
print "Sorted numbers:", numbers
There are surely more compact versions of that, but compactness is
usually less important than clarity, expecially for someone that is
learning Python.
This is a more compact version (with the suggestion by Wojciech Mula,
modified in two ways):

even = range(1, 10)[1::2]
odd = range(1, 10)[0::2]

print "Odd:", odd
print "Even:", even

numbers = even + odd
print "Numbers:", numbers
print "Sorted numbers:", sorted(numbers)
Bye,
bearophile

Apr 22 '06 #4
2006/4/22, Edward Elliott <no****@127.0.0.1>:
No substantive problems. The str() calls are unnecessary, print calls
list.__str__ already. You can replace the loop with list comprehensions or
slices. Same result, a bit more succinct. See these pages for more:

http://docs.python.org/lib/typesseq.html
http://docs.python.org/tut/node7.html (section 5.1.4)


Thnx, I didn't catch the use of colon in print...

Thanks to the other posting, but actually I want to write some code
that one can modify to his own needings

PAolo
Apr 23 '06 #5
"PAolo" <pa***********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
for i in range(1,10):
if i%2:
odd.append(i)
else:
even.append(i)


In 2.5 you'll be able to say

for i in range(1,10):
(odd if i%2 else even).append(i)

Whether you want to do this is another question entirely, of course.
Apr 25 '06 #6

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

Similar topics

12
by: Kin®sole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
4
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; }; ...
0
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...
3
by: Peter Olcott | last post by:
How does not specify the sort criteria for Generic.List ?? The way that this is done in C++ STL is to implement operator<(), how is this done in C# and DotNet for Generic.List ???
0
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...
31
by: John Salerno | last post by:
I posted this code last night in response to another thread, and after I posted it I got to wondering if I had misused the list comprehension. Here's the two examples: Example 1:...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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
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...

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.