Home > Enterprise >  If/Else Statements Inside a list definition?
If/Else Statements Inside a list definition?

Time:04-19

I've been using Python for a long time, but I've recently discovered a new way of creating and iterating through lists:

l = [x for x in range(0, 10)]

This produces a list from 0 to 9. It's very useful, and much simpler than what I had been doing before:

l = []

for x in range(0, 9):
    
    l.append(x)

Now that I understand this way of doing things, I've been using it for a lot of stuff, and it's working out great. The only problem is, I sometimes want another option. For example, if I have a list full of 1's and 0's:

import random

l = [random.randint(0, 1) for i in range(0, 10)]

I want to take this list and iterate through again, only do two different things based on an if else statement:

for idx, item in enumerate(l):

    if item == 1:

        l[idx] = 'A'

    else:

        l[idx] = 'B'

I know it would be easier to just create a list full of random instances of 'A' and 'B', but this is just an example, it won't work in my use case. How would I use my new way of creating lists to do this? I know how to add an if statement on the end:

l = [x for x in range(0, 10)]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l = [x for x in range(0, 10) if i != 7]
# [0, 1, 2, 3, 4, 5, 6, 8, 9]

But what about my else statement? And, incidentally, what is this way of creating lists called? I might have been able to find an answer online if I knew.

CodePudding user response:

What you want to do is called list comprehension and it is very helpful indeed!

This post answers your question: if/else in a list comprehension

And in your example, you want to do:

L = ['A' if random.randint(0, 1)==1 else 'B' for i in range(0, 10)]

CodePudding user response:

Ok so let's clear some things up. Firstly, this way of creating lists is called list comprehension.

Now, let's talk about how to achieve a list of A's and B's(by list comprehension):

import random

# This creates a list of 1's and 0's
l = [random.randint(0, 1) for i in range(0, 10)]
# [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]

# This creates a list of A's and B's
l = ['A' if num == 1 else 'B' for num in l]
#['A', 'A', 'A', 'B', 'B', 'A', 'A', 'A', 'A', 'B']

How to do it normally (without list comprehension)?

import random

l = []
for x in range(0, 10): # This should be 10 instead of 9 to get 10 values. 
    l.append(random.randint(0,1))
# l = [0, 1, 0, 1, 1, 0, 0, 0, 0, 1]

for idx, item in enumerate(l): # You need to use `enumerate()` to get index, value
    if item == 1:
        l[idx] = 'A'
    else:
        l[idx] = 'B'
# l = ['B', 'A', 'B', 'A', 'A', 'B', 'B', 'B', 'B', 'A']

CodePudding user response:

You could make use of Python's ternary operator:

l = ['A' if i == 1 else 'B' for i in l]

CodePudding user response:

The method of creating lists that you used is called a "list generator". Similarly, there is a dictionary generator.

To solve your problem, you need to add conditions inside the list generator:

import random

new_list = [random.randint(0, 1) for i in range(0, 10)]
another_new_list = ['A' if number == 1 else 'B' for number in new_list]
  • Related