saidozcan
saidozcan

Reputation: 2215

Finding an element in list

i am beginner with python and i have a list like this

 [{'status': 1L, 'permalink': 'said', 'title': 'a', 'author': 'said', 'content': 'a', 'created_on': datetime.datetime(2013, 2, 10, 17, 46, 9), 'type': 1L, 'id': 1L}, 
{'status': 1L, 'permalink': 'said', 'title': 'hello', 'author': 'said', 'content': 'this is my first post', 'created_on': datetime.datetime(2013, 2, 10, 17, 48, 5), 'type': 2L, 'id': 2L}]

I want to get the post which has the id 1.How can i do this.Should i use for loop or is there another simple way? I use Flask framework.

For example i got an id by calling url like this /editpost/1 then i want to get the post with the id 1.How can i do this? Also why it's integer 1L instead of 1? Thanks a lot.

Upvotes: 1

Views: 145

Answers (3)

albertoblaz
albertoblaz

Reputation: 590

You can try with 2 alternatives which are probably easier than your approach.

The first one is called "list comprehensions" and they are almost always the fastest way to iterate over an object (and in my opinion, the best). I'm not sure but I think they are valid for lists, tuples and dictionaries as well. So, you could write something like this:

// "posts" is the list of dictionaries you wrote in the question

my_post = [x for x in posts if x['id'] == 1L]

The second option is to use an approach from the Functional Programming and invoke the built-in filter function in Python, which has 2 arguments. The first one is a function to express the condition of the filter (actually, this thing is called a lambda expression) and the second argument is just the iterable object:

filter (lambda x : x['id'] == 1L, posts)

This way, the 'filter' function gets an element from 'posts' in each iteration, and passes it in as an argument for the lambda. Then, it checks if the current post's id values 1L, and if it's true, then the post is filtered and returned at the end of the loop.

I don't know if the second option is a bit advanced. Sorry in that case.

Hope it helps you! :)

Upvotes: 1

l4mpi
l4mpi

Reputation: 5149

While Martijn Pieters answers the question how you could find your data in a list, the actual answer to your question would be to use a database and query the database for the id. You should never mix code and data - this is exactly what a database is for.

But if you are absolutely certain that you don't want to use a database to store your posts in, it would be a good idea to create a dictionary indexing the posts by their id and simply querying the dictionary:

#construct the dictionary
posts_by_id = {post["id"]: post for post in list_of_posts}

#now getting a post is as simple as this:
mypost = posts_by_id[theid]

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1124000

1L is a python long type; it's just the same as a normal int, python treats it the same and the distinction doesn't matter.

To get a specific item that matches your key, I would use the next() function with a generator expression:

entryid = 1

try:
    match = next(s for s in inputlist if s['id'] == entryid)
except StopIteration:
    match = None  # not found

This will find the first dictionary that has the key id matching your entryid, or set match to None if there was no such item.

Upvotes: 2

Related Questions