Python Forum

Full Version: count unique values of a list with a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.)
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.
Ah I used {} instead of [].

If combined it would be:


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