2

I am using list in my first python project.

data_items = [{"ID":0x1111,  "Description":"data1",  "Writable":"FALSE"},
      {"ID":0x2222,  "Description":"data2",  "Writable":"TRUE"},
      {"ID":0x3333,   "Description":"data3", "Writable":"FALSE"}, 
      {"ID":0x4444,   "Description":"data4", "Writable":"TRUE"}]

I want to use another list name "new_data_items" and display only TRUE conditions like below example:How to write for loop for this

new_data_items = [{"ID":0x2222,  "Description":"data2",  "Writable":"TRUE"},
          {"ID":0x4444,   "Description":"data4", "Writable":"TRUE"}]
  • 1
    Possible duplicate of [List filtering: list comprehension vs. lambda + filter](http://stackoverflow.com/questions/3013449/list-filtering-list-comprehension-vs-lambda-filter) – hlt Apr 11 '17 at 11:55
  • `[item for item in data_items if item.get("Writable") == "TRUE"]` – Jared Smith Apr 11 '17 at 11:56

4 Answers4

4

You can use filter to do that:

filter(lambda x: x['Writable'] == 'TRUE', data_items)

Note that filter returns an iterator so you may also want to convert that to a list:

list(filter(lambda x: x['Writable'] == 'TRUE', data_items))

If the 'Writable' field may not be present for some records replace indexing with .get:

list(filter(lambda x: x.get('Writable') == 'TRUE', data_items))
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

use python list compression

[_ for _ in data_items if _.get('Writable') == 'TRUE']

Chk this link : https://repl.it/HEyl/0

Himanshu dua
  • 2,496
  • 1
  • 20
  • 27
  • 1
    Just curious, why would you use `_`? `_` is usually used for unneeded variables. – Eric Duminil Apr 11 '17 at 11:58
  • _ is used for temporary variables in python. When you don't care about variable name then its best to use '_' instead of using i,j – Himanshu dua Apr 11 '17 at 12:00
  • @Himanshudua actually I think Erik is right. Underscore usually indicates a variable that will not be used. – Ivaylo Strandjev Apr 11 '17 at 12:01
  • @IvayloStrandjev I don't know that – Himanshu dua Apr 11 '17 at 12:02
  • `_` isn't used for temporary variables. You can use `x, _, z = coordinates` if you don't care about `y` for example. But using `_` for an important variable makes your code less readable. – Eric Duminil Apr 11 '17 at 12:11
  • Yes, I agree with you @EricDuminil – Himanshu dua Apr 11 '17 at 12:27
  • In your example `_` is an importtant variable though, since it's the one you want to keep. – Eric Duminil Apr 11 '17 at 12:28
  • @EricDuminil In list compression, I prefer to use '_' over other variable names (item,data) because I don't care about that variable name in single line function of list compression.However, If I used a for loop here then I will prefer other variable names : item,data that will also improve readability of code – Himanshu dua Apr 11 '17 at 12:31
  • It's a list comprehension, not compression. You might not care about that variable name. Python doesn't care either, and executes the script just fine. SO users and other people reading your code do care a lot. Readers shouldn't have to think about which objects answer to `get()` to understand that `_` is a `dict` in your example. – Eric Duminil Apr 11 '17 at 12:54
1

You could do something like the following:

new_data_items = []
for item in data_items:
    if item['Writable'] == 'TRUE':
        new_data_items.append(item)

But list comprehensions would be quicker and less verbose:

new_data_items = [item for item in data_items if item['Writable'] == 'TRUE']
valeas
  • 364
  • 1
  • 7
  • 18
1
data_items = [{"ID":0x1111,  "Description":"data1",  "Writable":"FALSE"},
  {"ID":0x2222,  "Description":"data2",  "Writable":"TRUE"},
  {"ID":0x3333,   "Description":"data3", "Writable":"FALSE"},
  {"ID":0x4444,   "Description":"data4", "Writable":"TRUE"}]

new_data = list()
for element in data_items:
    value = element.get('Writable')
    if not value == "FALSE":
        new_data.append(element)

print new_data