Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:23:36+00:00 2026-06-03T17:23:36+00:00

I would like to loop through a big two dimension list: authors = [[Bob,

  • 0

I would like to loop through a big two dimension list:

authors = [["Bob", "Lisa"], ["Alice", "Bob"], ["Molly", "Jim"], ... ]

and get a list that contains all the names that occurs in authors.

When I loop through the list, I need a container to store names I’ve already seen, I’m wondering if I should use a list or a dict:

with a list:

seen = []
for author_list in authors:
    for author in author_list:
        if not author in seen:
            seen.append(author)
result = seen

with a dict:

seen = {}
for author_list in authors:
    for author in author_list:
        if not author in seen:
            seen[author] = True
result = seen.keys()

which one is faster? or is there better solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:23:37+00:00Added an answer on June 3, 2026 at 5:23 pm

    You really want a set. Sets are faster than lists because they can only contain unique elements, which allows them to be implemented as hash tables. Hash tables allow membership testing (if element in my_set) in O(1) time. This contrasts with lists, where the only way to check if an element is in the list is to check every element of the list in turn (in O(n) time.)

    A dict is similar to a set in that both allow unique keys only, and both are implemented as hash tables. They both allow O(1) membership testing. The difference is that a set only has keys, while a dict has both keys and values (which is extra overhead you don’t need in this application.)


    Using a set, and replacing the nested for loop with an itertools.chain() to flatten the 2D list to a 1D list:

    import itertools
    seen = set()
    for author in itertools.chain(*authors):
        seen.add(author)
    

    Or shorter:

    import itertools
    seen = set( itertools.chain(*authors) )
    

    Edit (thanks, @jamylak) more memory efficient for large lists:

    import itertools
    seen = set( itertools.chain.from_iterable(authors) )
    

    Example on a list of lists:

    >>> a = [[1,2],[1,2],[1,2],[3,4]]
    >>> set ( itertools.chain(*a) )
    set([1, 2, 3, 4])
    

    P.S. : If, instead of finding all the unique authors, you want to count the number of times you see each author, use a collections.Counter, a special kind of dictionary optimised for counting things.

    Here’s an example of counting characters in a string:

    >>> a = "DEADBEEF CAFEBABE"
    >>> import collections
    >>> collections.Counter(a)
    Counter({'E': 5, 'A': 3, 'B': 3, 'D': 2, 'F': 2, ' ': 1, 'C': 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to loop through a checked list box and see which values
I would like to loop through two lists using a For each loop. dim
I would like to be able to loop through a list which is a
how to loop through a $.post callback function? Here's the code I would like
I store post IDs in an array. I would like to loop through the
I would like to loop through an std::map and delete items based on their
I would like to loop through a string and use each character. int i
I would like to loop through an array strings and add them to an
I would like to loop through audio in VB.NET. Here is my code: While
I'm writing a jQuery validation plugin and would like to loop through some of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.