1

Given this dictionary

test = {"a": 0.8, "b": 0.9, "c": 1.0, "d": 1.1, "e": 1.2 }

I want to create two lists: list_a with dictionary values <1 and list_b with dictionary values >=1

list_a = [0.8 , 0.9]
list_b = [1.0 , 1.1 , 1.2] 

I'm using this code to convert the dictionary values to list

list_a = list(test.values())

but I don't know where to insert the IF statement. Is there a way to input the IF statement within the list function?

list_a = list(test.values() if test.values()>=1) #this is wrong
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
alz
  • 307
  • 2
  • 13
  • Possible duplicate of [if/else in a list comprehension?](https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension) – Patrick Artner Oct 27 '19 at 10:33

3 Answers3

4

Yes, use a list comprehension:

list_a = [a for a in test.values() if a >= 1]

In this case using [...] versus list(...) is equivalent, but that is not always true.

Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30
  • This is what I was looking for. Could you please elaborate more on "In this case using [...] versus list(...) is equivalent, but that is not always true." – alz Oct 27 '19 at 11:06
  • What are the drawbacks of using `[...]` instead of `list(...)`? – alz Oct 27 '19 at 11:09
  • Using `[...]` with a comprehension is a list comprehension. You can achieve the same with `list(...)` that contains a generator comprehension, but that is probably less optimized, as Python needs to first create the generator, and than convert it to a list. Whereas when using a list comprehension, Python just knows that it needs to create a list. (But maybe Python is clever enough to optimize that as well, but less sure. List comprehensions are definitely that advised pattern.) Off course, you need to use `list(...)` for just converting a regular generator to a list, like with `test.values()`. – Hielke Walinga Oct 27 '19 at 12:05
0

you can use this if you want.

test = {"a": 0.8, "b": 0.9, "c": 1.0, "d": 1.1, "e": 1.2 }
list_a = []
list_b = []
for i in test:
    if test[i] < 1:
        list_a.append(test[i])
    elif test[i] >= 1:
        list_b.append(test[i])

print(list_a)
print(list_b)
Mrrelaxed
  • 40
  • 1
  • 7
0

We turn dictionary into two lists, keys and values we need values

test = {"a": 0.8, "b": 0.9, "c": 1.0, "d": 1.1, "e": 1.2 }
keys, values = zip(*test.items())
print(values)

Output:

(0.8, 0.9, 1.0, 1.1, 1.2)

than appending elements in list with loop and condition:

list1=[]
list2=[]
for x in values:
  if x<1:
            list1.append(x)
  else:
            list2.append(x)

print(list1)
print(list2)

Output:

[0.8, 0.9]
[1.0, 1.1, 1.2]
Mouse on the Keys
  • 322
  • 1
  • 5
  • 13
  • this is just smoke: `keys, values = zip(*test.items())` - you do not need the keys and the values can be get directly: `text.values()` - removing this step it is the same as Infeamous99 answer - he uses the keys to index into the dict – Patrick Artner Oct 27 '19 at 11:35
  • @PatrickArtner posted it in same time as Infeamous99 and I didn`t read question till the end, he wanted it in one line, I wanted to explain porcess.. so thats it... – Mouse on the Keys Oct 27 '19 at 11:37