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
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 283 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Copying the order of another list with identical values gohanhango 7 1,141 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Sample random, unique string pairs from a list without repetitions walterwhite 1 457 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,244 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,172 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,576 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,160 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,540 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 919 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Adding values with reduce() function from the list of tuples kinimod 10 2,665 Jan-24-2023, 08:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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