This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- C
- C++
- C#
- Go
- Java
- JS
- Obj-C
- PHP
- Python
- Ruby
- Rust
- Or search :
Idiom #7 Iterate over list indexes and values
Print each index i with its value x from an array-like collection items
- Dart
- Dart
- Ada
- C
- Caml
- Clojure
- C++
- C++
- C#
- C#
- C#
- D
- D
- Elixir
- Erlang
- Fortran
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Perl
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
- VB
- Dart
items.asMap[].forEach[[i, value] {
print['index=$i, value=$value'];
}];
- Demo
- Doc
- Dart
for [var i = 0; i < items.length; i++] {
print['index=$i, value=${items[i]}'];
}
- Demo
- Ada
- C
- Caml
- Clojure
- C++
- C++
- C#
- C#
- C#
- D
- D
- Elixir
- Erlang
- Fortran
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Perl
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
- VB
with Ada.Text_IO;
use Ada.Text_IO;
for I in Items'Range loop
X := Items [I];
Put_Line [Integer'Image [I] & " " & Integer'Image [X]];
end loop;
for [size_t i = 0; i < n; i++] {
printf["Item %d = %s\n", i, toString[items[i]]];
}
[* output_elem is a printer for elements of [items] *]
items |> List.iteri [fun i x ->
printf "%d: %a" i output_elem x
]
[doseq [[i x] [map-indexed vector items]]
[println i ":" x]]
- Doc
#include
for [std::size_t ix = 0; ix < items.size[]; ++ix] {
std::cout $x]
{
echo "i=$i, x=$x"; echo '
'; }
'; }
var I:Integer;
Items: array of String;
[..]
for I := 0 to high[Items] do
WriteLn['Item ', I, ' = ', Items[I]];
uses Classes, SysUtils;
var I:Integer;
Items: array of TObject;
[...]
for I := 0 to high[Items] do
if assigned[Items[I]] then
WriteLn[Format['Item %d = %s', [I, Items[I].ToString]]];
use 5.012; # each @array
# For an array
while [my [$idx, $val] = each @array] {
print "array[$idx] = $val\n";
}
# For a hash
while [my [$key, $val] = each %hash] {
print "hash{$key} = $val\n";
}
- Doc
for i, x in enumerate[items]:
print i, x
- Doc
- Origin
items.each_with_index do |x, i|
puts "Item #{i} = #{x}"
end
items.each_index{|i| puts "Item %d = %s" % [i, items[i]]}
for [i, x] in items.iter[].enumerate[] {
println!["Item {} = {}", i, x];
}
- Demo
- Doc
items.iter[].enumerate[].for_each[|[i, x]| {
println!["Item {} = {}", i, x];
}]
- Demo
- Doc
val items = List["a", "b", "c"]
items.zipWithIndex.foreach{ case [item, index] =>
println[s"$index => $item"]
}
[define [display-list items]
[define [display-list items i]
[if [not [null? items]]
[begin
[display [string-append
[number->string i]
": "
[number->string [first items]]
"\n"]]
[display-list [rest items] [+ i 1]]]
'done]]
[display-list items 0]]
items withIndexDo: [:item :index |
Transcript showln: 'item: ' , item asString , ' index: ' , index asString].
Imports System
For i As Integer = 0 To items.Length - 1
Dim x = items[i]
Console.WriteLine[$"Item {i} = {x}"]
Next
- Demo
Imports System
For i As Integer = 0 To items.Count - 1
Dim x = items[i]
Console.WriteLine[$"Item {i} = {x}"]
Next
- Demo
Do you know the best way to do this in your language ? New implementation...
Idiom created by programming-idioms.org
History- View revisions
- Iterate over list values
- Create a new list
- Add an element at the end of a list
- Traverse list backwards
- Iterate over characters of a string
Cheatsheets
Issues- Report a bug