Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops in List
#11
(Mar-12-2017, 10:42 AM)ichabod801 Wrote:
(Mar-12-2017, 04:43 AM)Skaperen Wrote: it had been my understanding that print() would apply repr() to any non-string argument it got.

It does. The error happens before that point. First it evaluates the argument it gets, then it applies repr. The error comes in the evaluation, when + listA[i] tries to add an integer to a string.

Edit: Didn't think it through. print applies str() to any non-string argument it gets. Typing an argument into the interactive console gives the repr().

my bad for blaming it on print().  the OP was building a big string with an int in there and i didn't look at the code close enough to see that.  separate args given to print() works, too.

now you guys need to advise the OP what is the best way to code this to avoid that perl-ism mentioned earlier.

Output:
lt1/forums /home/forums 2> cat foo.py # loop in list   listA = list(range(0,101,5))   for i in range(len(listA)):     print('Item no.', i+1,  'is:', listA[i]) lt1/forums /home/forums 3> py3 foo.py Item no. 1 is: 0 Item no. 2 is: 5 Item no. 3 is: 10 Item no. 4 is: 15 Item no. 5 is: 20 Item no. 6 is: 25 Item no. 7 is: 30 Item no. 8 is: 35 Item no. 9 is: 40 Item no. 10 is: 45 Item no. 11 is: 50 Item no. 12 is: 55 Item no. 13 is: 60 Item no. 14 is: 65 Item no. 15 is: 70 Item no. 16 is: 75 Item no. 17 is: 80 Item no. 18 is: 85 Item no. 19 is: 90 Item no. 20 is: 95 Item no. 21 is: 100 lt1/forums /home/forums 4>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
Hm!
@scaperen see my post from yesterday. When I wrote this I didn't try it with @SnadraDan's loop. Noone iterate over a list like this: for i in range(len(listA)):. So I've used for i in listA: even without thinking. Which is not working just separating the values as I've proposed previously. But it works if I use range(len(list)).

Well, when I was in the beginning with Python I used to do it in the same way. Looping over the number of the elements in the list as an index to that list. My brief C experience many years ago was the reason for this. So, how to do it in Python... With enumerate() built-in function.

I can't understand what you mean with Perl-ism. I don't know Perl at all. Anyway, here it is. The Pythonic way. Pfff, sounds religious  Dodgy
>>> l = list(range(0, 50000, 5))

>>> for index, element in enumerate(l, start=1):
...     print("Item No. {} is {}.".format(index, element))
Output:
.................. .................. Item No. 9991 is 49950. Item No. 9992 is 49955. Item No. 9993 is 49960. Item No. 9994 is 49965. Item No. 9995 is 49970. Item No. 9996 is 49975. Item No. 9997 is 49980. Item No. 9998 is 49985. Item No. 9999 is 49990. Item No. 10000 is 49995.
The format() parameters are positional or key=value. The parameter can be an expression. The replacement of the parameters into the string is in the same order. But it is not necessary. 

>>> print("February in {1} has {0} days.".format(28, 2017))

February in 2017 has 28 days.

>>> print("February in {year} has {days} days.".format(days=28, year=2017))

February in 2017 has 28 days.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
the perl-ism is: there is more than one way to do it as i mentioned in post 3.  python can appear to be this way.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
(Mar-14-2017, 05:57 AM)Skaperen Wrote: the perl-ism is: there is more than one way to do it as i mentioned in post 3.  python can appear to be this way
But the culture of Python is not that way. If you do import this, one of the many things you will see is:
Output:
There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
(Mar-14-2017, 10:31 AM)ichabod801 Wrote: But the culture of Python is not that way. If you do import this, one of the many things you will see is:
Output:
There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch.

exactly.  this is why i urge that answers give an explanation instead of just posting another way to do it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
(Mar-14-2017, 05:57 AM)Skaperen Wrote: the perl-ism is: there is more than one way to do it as i mentioned in post 3.  python can appear to be this way.

It's more than that, though.  Ther perl way, is that there are many ways to do something, and they're all equally valid.  In python, although there might be multiple ways to do something, most of them will be ugly as hell and obvious that you were trying to find "another" way to do it.
Reply
#17
i would say that all ways to do things are ugly in perl.  i did give perl a try a very long time ago (around 1996).  it was a short try.  i was still heavy in C so my perl was very much C-like ... and still ugly.  it was pike that got me a step away from C so that when i tried python i could do things less C like.  the first way i loop over a list is like for tag in tags: (where tags is a list).  i have never used enumerate().

i did not even know of enumerate().  there might be 2  or 3 cases in my code files where it could be used in place of counting.  maybe one day i will look and see if is worthwhile.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#18
The unwritten rule is simple,never use range(len(sequence)).
If need index or manipulate index always enumerate()
Eg:
# Find index of Lemon in list
lst = ['Apple', 'Lemon', 'Orange']
for index, item in enumerate(lst):
    if item == 'Lemon':
        print(f'Lemon is at index: {index}')
Output:
Lemon is at index: 1
We see this way in more places eg iterate over dict:
emails = {
     'Bob': '[email protected]',
     'Alice': '[email protected]',
 }

for name, email in emails.items():
    print(f'{name} -> {email}')
Output:
Bob -> [email protected] Alice -> [email protected]
A couple of video:
Transforming Code into Beautiful, Idiomatic Python
Loop like a native: while, for, iterators, generators
Reply
#19
i don't use range(len(sequence)) to iterate that sequence.  i think i recall one use of range(len(sequence)) and it was to make an iterator to recreate a similar sequence in a function (it was py2 and i used xrange).  that code would be very different if i re-did it today.  all my code would evolve if i re-did everything every day.  but i don't have enough time for that.  i will re-do a lot of my code, eventually, as i have resolved the last issue preventing me from doing everything in py3 (finally got botocore working correctly in py3 ... a lot of my code uses boto or botocore).

i also do a lot of deeper system stuff and embedded stuff.  but i don't need C for that (or at least not for most of it ... i do for my set-top-box boot loaders) anymore.

(Mar-20-2017, 11:28 AM)snippsat Wrote: The unwritten rule is simple,never use range(len(sequence)).
If need index or manipulate index always enumerate()
Eg:
# Find index of Lemon in list
lst = ['Apple', 'Lemon', 'Orange']
for index, item in enumerate(lst):
    if item == 'Lemon':
        print(f'Lemon is at index: {index}')
Output:
Lemon is at index: 1

why not index=lst.index('Lemon') instead of a loop?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#20
What if there's more than one Lemon? Do you just want the first index, or all of them?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 824 Sep-18-2023, 07:52 AM
Last Post: buran
  Using recursion instead of for loops / list comprehension Drone4four 4 3,072 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Help with List Loops slackerman73 4 2,680 Nov-25-2019, 07:49 AM
Last Post: perfringo
  Adding items in a list (using loops?) Seneca260 6 2,705 Nov-22-2019, 11:34 AM
Last Post: Seneca260
  simple list check with loops Low_Ki_ 23 15,161 Jan-09-2017, 03:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020