Python Forum
Learning python specific syntax after using other scripting languages for years
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning python specific syntax after using other scripting languages for years
#10
(Dec-12-2017, 12:30 AM)Arcs Wrote: hopefully one day python can have a powerful foreach loop as it is in many ways my love letter to PHP

Not quite sure you understand that python HAS it...
i.e. your example in OP, is the same as printing key, value pairs from dict as already shown in previous posts, but anyway

PHP
$a = array('one'=>2,3=>4,5=>'six');
foreach($a as $k=>$v) echo "$k:$v\n";
Python
a = {'one':2,3:4,5:'six'}
for key, value in a.items():
    print('{}:{}'.format(key, value))
now if you want also the index, then you need enumerate
same apply for any other iterable. Actually this is the pythonic way, not using indexes
Reply


Messages In This Thread
RE: Learning python specific syntax after using other scripting languages for years - by buran - Dec-12-2017, 11:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting days to years in loop while computing values across grid cells Lightning1800 2 2,682 May-15-2018, 08:44 PM
Last Post: Lightning1800

Forum Jump:

User Panel Messages

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