Python Forum
count unique values of a list with a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
count unique values of a list with a list
#1
so I'm fiddling with lists ... (and yup, new in python)

mylist = ["a", "b", "c"]
where "a,b,c" are lists with some values too ... like
"a" > {"longname", "1234"}
"b" > {"longname", "1223434"}
"c" > {"longnaaame", "12314444223234"}
keyword = "name"


I want my primary list to be checked with unique values on the nested list values.
So in this example the result would be '2' since 'name' occurs in 'a' and in 'b'.

Afterwards I could use len to check.

I'm thinking
ulist = []
for l in mylist:
> for nl in l
>> if keyword in nl:
>>> ulist.append(l)
Makes sense? I hope so. This the way to do so?

(later on I want to check if there is 'one' item in the list ... if so ... do something ... else ... etc.)
Reply
#2
I'm struggling a bit to understand what you are saying.

"a" is a string literal, so can't contain anything other than string characters.

{"longname", "1234"} is a dictionary.

You can have:

a = {"longname", "1234"}
b = {"longname", "1223434"}
c = {"longnaaame", "12314444223234"}
mylist = [a, b, c]
If so, you are therefore checking if the string "name" appears within the only key in each list entry dictionary? Counting unique values doesn't make sense as more than one is not unique.

Perhaps you can clarify things a little.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
Ah I used {} instead of [].

If combined it would be:


mylist = [["longname", "1234"], ["longname", "1223434"], ["longnaaame", "12314444223234"]]
(reading and learning the difference between (), [], {} Angel )
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove duplicates from dicts with list values wardancer84 27 1,338 May-27-2024, 04:54 PM
Last Post: wardancer84
  Strange behavior list of list mmhmjanssen 3 547 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 577 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Copying the order of another list with identical values gohanhango 7 1,425 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Sample random, unique string pairs from a list without repetitions walterwhite 1 622 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,459 Nov-03-2023, 05:35 PM
Last Post: huzzug
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,453 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 7,128 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,463 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,866 May-01-2023, 09:06 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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