Python For-In Loop Preceded by a Variable

Python for-in loop preceded by a variable

The current answers are good, but do not talk about how they are just syntactic sugar to some pattern that we are so used to.

Let's start with an example, say we have 10 numbers, and we want a subset of those that are greater than, say, 5.

>>> numbers = [12, 34, 1, 4, 4, 67, 37, 9, 0, 81]

For the above task, the below approaches below are totally identical to one another, and go from most verbose to concise, readable and pythonic:

Approach 1

result = []
for index in range(len(numbers)):
if numbers[index] > 5:
result.append(numbers[index])
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 2 (Slightly cleaner, for-in loops)

result = []
for number in numbers:
if number > 5:
result.append(number)
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 3 (Enter List Comprehension)

result = [number for number in numbers if number > 5]

or more generally:

[function(number) for number in numbers if condition(number)]

where:

  • function(x) takes an x and transforms it into something useful (like for instance: x*x)
  • if condition(x) returns any False-y value (False, None, empty string, empty list, etc ..) then the current iteration will be skipped (think continue). If the function return a non-False-y value then the current value makes it to the final resultant array (and goes through the transformation step above).

To understand the syntax in a slightly different manner, look at the Bonus section below.

For further information, follow the tutorial all other answers have linked: List Comprehension


Bonus

(Slightly un-pythonic, but putting it here for sake of completeness)

The example above can be written as:

result = filter(lambda x: x > 5, numbers)

The general expression above can be written as:

result = map(function, filter(condition, numbers)) #result is a list in Py2

Python: variables assigned before for loop

List comprehensions are a tool for transforming one list (any iterable actually) into another list. During this transformation, elements can be conditionally included in the new list and each element can be transformed as needed.

If you’re familiar with functional programming, you can think of list comprehensions as syntactic sugar for a filter followed by a map:

>>> doubled_odds = map(lambda n: n * 2, filter(lambda n: n % 2 == 1, numbers))
>>> doubled_odds = [n * 2 for n in numbers if n % 2 == 1]

Every list comprehension can be rewritten as a for loop but not every for loop can be rewritten as a list comprehension.

If you can rewrite your code to look just like this for loop, you can also rewrite it as a list comprehension:

new_things = []
for ITEM in old_things:
if condition_based_on(ITEM):
new_things.append("something with " + ITEM)

You can rewrite the above for loop as a list comprehension like this:

new_things = ["something with " + ITEM for ITEM in old_things if condition_based_on(ITEM)]    

Python for loop control variable accessed before declaration

This comprehension would look more like the following, in regular code (using a list to store our non-punctuation characters).

#input is defined somewhere prior to the loop
output = []
for char in input:
if not unicodedata.category(char).startswith('P'):
output.append(char)
''.join(output)

Comprehensions iterate over the loop portion first, with the value being iterated over on the left.

Get index value in python list comprehension (for in preceded with variable)

Probably you have meant indexes, as there are no keys in list. We can get them like this:

indexes = [index for index,element in enumerate(your_list)]

But this isn't really necessary, we can get length of the list and get indexes from it as indexes are from 0 to [length of list]-1:

length = len(your_list)
indexes = list(range(length))

Creating a new file, filename contains loop variable, python

Simply construct the file name with + and str. If you want, you can also use old-style or new-style formatting to do so, so the file name can be constructed as:

"file_" + str(i) + ".dat"
"file_%s.dat" % i
"file_{}.dat".format(i)

Note that your current version does not specify an encoding (you should), and does not correctly close the file in error cases (a with statement does that):

import io
for i in xrange(10):
with io.open("file_" + str(i) + ".dat", 'w', encoding='utf-8') as f:
f.write(str(func(i))

Why does python use 'else' after for and while loops?

It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:

found_obj = None
for obj in objects:
if obj.key == search_key:
found_obj = obj
break
else:
print('No object found.')

But anytime you see this construct, a better alternative is to either encapsulate the search in a function:

def find_obj(search_key):
for obj in objects:
if obj.key == search_key:
return obj

Or use a list comprehension:

matching_objs = [o for o in objects if o.key == search_key]
if matching_objs:
print('Found {}'.format(matching_objs[0]))
else:
print('No object found.')

It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.

See also [Python-ideas] Summary of for...else threads

Python for-in loop preceded by a variable

The current answers are good, but do not talk about how they are just syntactic sugar to some pattern that we are so used to.

Let's start with an example, say we have 10 numbers, and we want a subset of those that are greater than, say, 5.

>>> numbers = [12, 34, 1, 4, 4, 67, 37, 9, 0, 81]

For the above task, the below approaches below are totally identical to one another, and go from most verbose to concise, readable and pythonic:

Approach 1

result = []
for index in range(len(numbers)):
if numbers[index] > 5:
result.append(numbers[index])
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 2 (Slightly cleaner, for-in loops)

result = []
for number in numbers:
if number > 5:
result.append(number)
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 3 (Enter List Comprehension)

result = [number for number in numbers if number > 5]

or more generally:

[function(number) for number in numbers if condition(number)]

where:

  • function(x) takes an x and transforms it into something useful (like for instance: x*x)
  • if condition(x) returns any False-y value (False, None, empty string, empty list, etc ..) then the current iteration will be skipped (think continue). If the function return a non-False-y value then the current value makes it to the final resultant array (and goes through the transformation step above).

To understand the syntax in a slightly different manner, look at the Bonus section below.

For further information, follow the tutorial all other answers have linked: List Comprehension


Bonus

(Slightly un-pythonic, but putting it here for sake of completeness)

The example above can be written as:

result = filter(lambda x: x > 5, numbers)

The general expression above can be written as:

result = map(function, filter(condition, numbers)) #result is a list in Py2

Python for-in loop preceded by a variable

The current answers are good, but do not talk about how they are just syntactic sugar to some pattern that we are so used to.

Let's start with an example, say we have 10 numbers, and we want a subset of those that are greater than, say, 5.

>>> numbers = [12, 34, 1, 4, 4, 67, 37, 9, 0, 81]

For the above task, the below approaches below are totally identical to one another, and go from most verbose to concise, readable and pythonic:

Approach 1

result = []
for index in range(len(numbers)):
if numbers[index] > 5:
result.append(numbers[index])
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 2 (Slightly cleaner, for-in loops)

result = []
for number in numbers:
if number > 5:
result.append(number)
print result #Prints [12, 34, 67, 37, 9, 81]

Approach 3 (Enter List Comprehension)

result = [number for number in numbers if number > 5]

or more generally:

[function(number) for number in numbers if condition(number)]

where:

  • function(x) takes an x and transforms it into something useful (like for instance: x*x)
  • if condition(x) returns any False-y value (False, None, empty string, empty list, etc ..) then the current iteration will be skipped (think continue). If the function return a non-False-y value then the current value makes it to the final resultant array (and goes through the transformation step above).

To understand the syntax in a slightly different manner, look at the Bonus section below.

For further information, follow the tutorial all other answers have linked: List Comprehension


Bonus

(Slightly un-pythonic, but putting it here for sake of completeness)

The example above can be written as:

result = filter(lambda x: x > 5, numbers)

The general expression above can be written as:

result = map(function, filter(condition, numbers)) #result is a list in Py2

What is the pythonic way to detect the last element in a 'for' loop?

Most of the times it is easier (and cheaper) to make the first iteration the special case instead of the last one:

first = True
for data in data_list:
if first:
first = False
else:
between_items()

item()

This will work for any iterable, even for those that have no len():

file = open('/path/to/file')
for line in file:
process_line(line)

# No way of telling if this is the last line!

Apart from that, I don't think there is a generally superior solution as it depends on what you are trying to do. For example, if you are building a string from a list, it's naturally better to use str.join() than using a for loop “with special case”.


Using the same principle but more compact:

for i, line in enumerate(data_list):
if i > 0:
between_items()
item()

Looks familiar, doesn't it? :)


For @ofko, and others who really need to find out if the current value of an iterable without len() is the last one, you will need to look ahead:

def lookahead(iterable):
"""Pass through all values from the given iterable, augmented by the
information if there are more values to come after the current one
(True), or if it is the last value (False).
"""
# Get an iterator and pull the first value.
it = iter(iterable)
last = next(it)
# Run the iterator to exhaustion (starting from the second value).
for val in it:
# Report the *previous* value (more to come).
yield last, True
last = val
# Report the last value.
yield last, False

Then you can use it like this:

>>> for i, has_more in lookahead(range(3)):
... print(i, has_more)
0 True
1 True
2 False


Related Topics



Leave a reply



Submit