-4

My lists looks like this (those numbers are UIDS):

sysusers = [['user1'],[7972],
           ['user2'],[2121],
           ['hacker'],[2132]
           ['hacker2'],[1232]]
users = [['user1'],[7972],
        ['user2'],[2121]]

I want to take the difference of those two lists and output it in a list similar to this:

badusers = [hacker,
           hacker2]
Safe G
  • 13
  • 3
  • 3
    Ok so do that then – SuperStew Feb 01 '18 at 19:02
  • Show us what you've tried so far and what doesn't work, that way we can help you – wohe1 Feb 01 '18 at 19:02
  • 3
    `users = [['user1'][7972],['user2'][2121]]` is not valid syntax – MooingRawr Feb 01 '18 at 19:04
  • @MooingRawr Not true. – Stefan Pochmann Feb 01 '18 at 19:05
  • In relation to what @MooingRawr said, you might want to use an `array` of `dictionaries` instead to better store your data. So something like: `users = [{ 'uid' : 7972, 'username' : 'user1' }, { 'uid' : 2121, 'username' : 'user2' }]`. – Whooper Feb 01 '18 at 19:09
  • 1
    @StefanPochmann In theory it's valid syntax if the UIDs are slices (e.g. `['astring'][3]) == 'r'`) but, even if you weren't getting IndexErrors left and right, you'd end up with a list of strings (e.g. `['u','i,'h']`). It's puzzling why you chose this type of data structure when, say, sets of tuples or a dictionary {UID: name} would serve you much better. – offbyone Feb 01 '18 at 19:14
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Feb 01 '18 at 19:16
  • 1
    @Whooper nitpick: that's a *list* not an array. – juanpa.arrivillaga Feb 01 '18 at 19:19
  • 2
    @offbyone It's valid syntax, period. Not just in theory (what is that even supposed to mean?). And huh? It's puzzling why I chose this type of data structure? I didn't. – Stefan Pochmann Feb 01 '18 at 19:19
  • Oh yes you are right! Thanks for the correction @juanpa.arrivillaga :D – Whooper Feb 01 '18 at 19:20
  • @StefanPochmann I think the _It's puzzling why you chose this type of data structure_ part is addressed to OP. And yes it definitely is a valid syntax! – Whooper Feb 01 '18 at 19:26

1 Answers1

1

The first problem you have is the way the lists are structured.

There needs to be a comma separation between each list.

sysusers = [['user1'],[7972],
           ['user2'],[2121],
           ['hacker'],[2132],
           ['hacker2'],[1232]]

users = [['user1'],[7972],
        ['user2'],[2121]]

Once this is correct, accessing the data you require is simple.

goodusers = []

for i in users:
    for j in i:
        for k in sysusers:
            for l in k:
                if l == j:
                    goodusers.append(k)

badusers = [x for x in sysusers if x not in goodusers]
print(badusers)

[['hacker'], [2132], ['hacker2'], [1232]]
johnashu
  • 2,167
  • 4
  • 19
  • 44