About 4,840,000 results
Open links in new tab
  1. Iterate over a list in Python - GeeksforGeeks

    Jan 2, 2025 · Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop . This method allows us to access each element in the list directly.

  2. 7 Ways to Loop Through a List in Python - LearnPython.com

    Jul 29, 2022 · 7 Ways You Can Iterate Through a List in Python 1. A Simple for Loop. Using a Python for loop is one of the simplest methods for iterating over a list or any other sequence (e.g. tuples, sets, or dictionaries). Python for loops are a powerful tool, so it is important for programmers to understand their versatility. We can use them to run the ...

  3. python - loop through each element in a list - Stack Overflow

    How would you loop through each element in a list in python. For example if I have the list: I would like to have the split up the list into: ['a','b','c',3,10,11,12] Right now I have: l = [] for item in list: l.append(item) return l. However this just returns the exact same list.

  4. Python foreach equivalent - Stack Overflow

    Oct 31, 2016 · For an updated answer you can build a forEach function in Python easily: def forEach(list, function): for i, v in enumerate(list): function(v, i, list) You could also adapt this to map, reduce, filter, and any other array functions from other languages or …

  5. Is there a 'foreach' function in Python 3? - Stack Overflow

    Aug 18, 2013 · Every occurence of "foreach" I've seen (PHP, C#, ...) does basically the same as pythons "for" statement. These are more or less equivalent: print($val); console.writeline(val); print(val) So, yes, there is a "foreach" in python. It's called "for". What you're describing is an "array map" function.

  6. Python For Loops - W3Schools

    With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. Print each fruit in a fruit list: The for loop does not require an indexing variable to set beforehand. Even strings are iterable objects, they contain a sequence of characters: Loop through the letters in the word "banana":

  7. Python Foreach - How to Implement - GeeksforGeeks

    Oct 13, 2024 · In this article, we will explore different ways to implement a foreach-like loop in Python, along with examples to illustrate their usage. 1. Using the For Loop using Python 'in' The most straightforward way to iterate over elements in a collection in Python is by using the for loop.

  8. Python - Loop Lists - W3Schools

    Use the range() and len() functions to create a suitable iterable. Print all items by referring to their index number: The iterable created in the example above is [0, 1, 2]. You can loop through the list items by using a while loop.

  9. Access List Items in Python - GeeksforGeeks

    Dec 16, 2024 · Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from 0. Other methods that we can we use to access list elements are : List comprehension is a more advanced and efficient way to access list items.

  10. Python List For Loop

    Python List For Loop iterates over each of the element in the list, and executes a block of statements for each element. In this tutorial, we have examples demonstrating the usage of For Loop on Python List.

Refresh