Python Forum
extracting second values elements only in every keys in an OrderedDict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extracting second values elements only in every keys in an OrderedDict
#1
i have this code, i want to extract the second values elements only in every keys in an OrderedDict,
i dont want to use for loop,any idea on this ?
from collections import OrderedDict

regDict= OrderedDict()
regDict["glenn"] = (100,200)
regDict["elena"] = (10,20)
print("values",list(regDict.values())[0][1])
print("values",list(regDict.values())[1][1])
print:
values 200
values 20
target output:
values 200,20    # or values ( 200,20 )
Reply
#2
Straight forward with a list comprehension:
from collections import OrderedDict

regDict = OrderedDict([('glenn', (100, 200)), ('elena', (10, 20))])
second_values = [vals[1] for vals in regDict.values()]
There are other methods like using itemgetter and map.
from collections import OrderedDict
from operator import itemgetter

regDict = OrderedDict([('glenn', (100, 200)), ('elena', (10, 20))])
getter = itemgetter(1)
second_values = list(map(getter, regDict.values()))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
glennford49 Wrote:i dont want to use for loop
Why don't you want to use for loop? Where does this requirement come from?
Reply
#4
(Nov-11-2020, 02:25 PM)Gribouillis Wrote: Where does this requirement come from?

From the teacher, I guess.
What I posted with map is also a loop, but hidden in C.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
thanks, +1, this saves my day
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Extracting Elements From A Website List knight2000 2 2,182 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,549 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  How to escape OrderedDict as an argument? Mark17 2 1,990 Dec-23-2020, 06:47 PM
Last Post: Mark17
  Adding keys and values to a dictionary giladal 3 2,429 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts Kristenl2784 4 7,267 Jul-17-2020, 01:50 AM
Last Post: palladium
  collections.OrderedDict Bert123456 2 1,748 Jul-09-2020, 08:51 PM
Last Post: Bert123456
  access dictionary with keys from another and write values to list redminote4dd 6 3,161 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  KeyError in OrderedDict rberna 1 2,082 Apr-01-2020, 04:42 AM
Last Post: perfringo
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,014 Dec-17-2019, 08:00 PM
Last Post: jasonashaw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020