473,507 Members | 3,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused: appending to a list

I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.
Can anyone explain this?
Thanks.
R.D.
# Start an empty list
list = []
while len(list) < 5:
# Get a random number between 1 & 100
num = random.randint(1,100)
# Make sure there are no duplicates
if num not in list:
# Append each number to the list
list.append(num)
print list

Mar 23 '06 #1
7 1299
DataSmash wrote:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.
Can anyone explain this?


Yes - you loop until the condition is _not_ fullfilled anymore. Which means
that if the list has a length of five, the len(l) < 6 is true, and
appending makes it len(l) == 6 - which then will fail your condition.

So - you need to loop one time less, by doing

while len(l) < 6 - 1:
...

diez
Mar 23 '06 #2
Em Qui, 2006-03-23 Ã*s 08:31 -0800, DataSmash escreveu:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.


"while len(list) < 5:" implies that the loop will stop only when
len(list) >= 5.

HTH,

--
Felipe.

Mar 23 '06 #3
DataSmash:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
Because the last time when len(list) was < 5, the block of code following
the while executed and did something to the list to give it a length >= 5
(otherwise the block of code would be executed again and it wouldn't have
been the last time).
list.append(num)


There you have it.

--
René Pijlman

Wat wil jij leren? http://www.leren.nl
Mar 23 '06 #4
You're wanting it to stop when the len(list) == 4, right? The easiest
way to change the logic would be to say

while len(list) != 4:

but that could get you into trouble later on. The problem with the
len(list) < 5 expression is that the loop will run "one more time" as
long as len(list) == 4 adding another item to the list giving you one
more than you wanted. If you wanted, you could put your len() check
inside the loop:

# Start an empty list
list = []
while 1:
# Get a random number between 1 & 100
num = random.randint(1,100)
# Make sure there are no duplicates
if num not in list:
# Append each number to the list
list.append(num)
if len(list) == 4:
# Break if we've reached desired length.
break
print list

I hope this gives you some ideas.

Mar 23 '06 #5

R.D.> I'm confused. Why is it that when I say "while len(list) < 5:", I
R.D.> get 5 items in my list. If I say "while len(list) < 6:", I get 6
R.D.> items in the list and so on. I would think if I said "less than
R.D.> 5", I would get 4 items. Can anyone explain this? Thanks. R.D.

The loop exits when the condition becomes false. That happens when
len(list) >= 5 (or 6).

Skip
Mar 23 '06 #6
"DataSmash" wrote:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.


except that you're saying "as long as there are less than 5 items
in the list, add another one"

</F>

Mar 23 '06 #7
Thanks for explaining and all the additional ideas!
R.D.

Mar 23 '06 #8

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

Similar topics

4
3173
by: bucket79 | last post by:
Hi is there anyway appending to dictionary? list has this feature >>>a = >>>a.append(1) >>>print a but dictionary can't
5
1878
by: rbt | last post by:
I know how to setup an empty list and loop thru something... appending to the list on each loop... how does this work with dicts? I'm looping thru a list of files and I want to put the file's...
0
1102
by: GujuBoy | last post by:
i have an array in C++ (array) and i want to append that to the list can i just say list1.append(array) without actually traversing through the entire array and appending... how can i do...
1
2280
by: Ron Adam | last post by:
In my program I have a lot of statements that append elements, but sometimes I don't want to append the element so it requres an if statement to check it, and that requires assigning the returned...
5
1641
by: John Salerno | last post by:
Here's the code: class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY) panel = wx.Panel(self) dbconn = self.connect_db() dbconn.execute("select namefirst,...
10
15000
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
2
6795
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
4
1479
by: John [H2O] | last post by:
I have a glob.glob search: searchstring = os.path.join('path'+'EN*') files = glob.glob(searchstring) for f in files: print f ___ This returns some files:
13
3208
by: yueying53 | last post by:
I am new to creating a list of classes. Hence I checked out this website: http://www.daniweb.com/code/snippet390.html. However, I modified the code to check if a class with a particular has been...
0
7223
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
7110
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
7372
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...
1
7030
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
7482
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...
0
5623
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,...
0
3191
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...
0
1540
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
758
muto222
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.