Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Best way to add a "position" value to each item in a list
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jerry Hill, post: 3897212"] Normally you wouldn't mutate the items in a list with a list comprehension. Instead, you would use a for loop, like this: for idx,item in enumerate(my_list_of_dicts): item['position'] = idx Is there a particular reason you want to do this with a list comprehension? Using a list comp means you're going to create an extra copy of your 10 million item list, just so you can add a key to each member, then (probably) throw away the original list. It doesn't seem like the right tool for the job here. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Best way to add a "position" value to each item in a list
Top