Python Forum
Getting Unique values from LISTS
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Unique values from LISTS
#2
This is not a single list, but numerous lists. So hence i grouped them together in a single list.

lists = [
['scext', '80.0'],
['scext', 'COLFUJDX440_DS11'],
['scext', 'Hard disk 1'],
['scext', '100.0'],
['scext', 'COLFUJDX440_DS11'],
['scext', 'Hard disk 2'],
['scext', '200.0'],
['scext', 'COLFUJDX440_DS11'],
['scext', 'Hard disk 3'],
['SCEXTVMD', '100.0'],
['SCEXTVMD', 'COLHG600P01_DS33'],
['SCEXTVMD', 'Hard disk 1'],
['SCEXTVMD', '40.0'],
['SCEXTVMD', 'COLHG600P01_DS33'],
['SCEXTVMD', 'Hard disk 2'],
['SCEXTVMD', '100.0'],
['SCEXTVMD', 'COLHG600P01_DS33'],
['SCEXTVMD', 'Hard disk 3'] 
]

d = {}
for lst in lists:
    key = lst[0]
    value = lst[1]
    d.setdefault(key, []).append(value)
    
print(d)
Output:
{'scext': ['80.0', 'COLFUJDX440_DS11', 'Hard disk 1', '100.0', 'COLFUJDX440_DS11', 'Hard disk 2', '200.0', 'COLFUJDX440_DS11', 'Hard disk 3'], 'SCEXTVMD': ['100.0', 'COLHG600P01_DS33', 'Hard disk 1', '40.0', 'COLHG600P01_DS33', 'Hard disk 2', '100.0', 'COLHG600P01_DS33', 'Hard disk 3']}
or run it through pretty printer:
Output:
{'SCEXTVMD': ['100.0', 'COLHG600P01_DS33', 'Hard disk 1', '40.0', 'COLHG600P01_DS33', 'Hard disk 2', '100.0', 'COLHG600P01_DS33', 'Hard disk 3'], 'scext': ['80.0', 'COLFUJDX440_DS11', 'Hard disk 1', '100.0', 'COLFUJDX440_DS11', 'Hard disk 2', '200.0', 'COLFUJDX440_DS11', 'Hard disk 3']}
The dictionary is close to the format you want, but has organized it near that format. You just have to print it out as you like.
Recommended Tutorials:
Reply


Messages In This Thread
Getting Unique values from LISTS - by aankrose - Oct-16-2019, 08:37 PM
RE: Getting Unique values from LISTS - by metulburr - Oct-16-2019, 09:28 PM
RE: Getting Unique values from LISTS - by aankrose - Oct-17-2019, 05:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 418 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 339 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  user input values into list of lists tauros73 3 1,107 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,452 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,655 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Inserting values from multiple lists sqlite azulu 1 2,528 May-24-2020, 08:40 AM
Last Post: ibreeden
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 2,090 May-08-2020, 12:04 PM
Last Post: anbu23
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 4,316 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Why does this list of lists end up with the same values alehak 2 2,641 Jul-18-2019, 12:34 PM
Last Post: perfringo
  Generate unique random numbers from different lists Takeshio 5 3,830 May-24-2019, 07:29 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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