0

I have a large list that contains 30000 other lists. The large list contains a list of a string and two other lists. Here is an example:

large_list = [
    ...
    ['maj', [4, 7], ['3', '5']],
    ['maj7', [4, 7, 11], ['3', '5', '7']],
    ...
    ]

I want to iterate through this large_list and find the smaller list by looking for the name (maj, maj7, etc.).

What is the fastest way of doing this?

I had thought to put the list in a .txt file and reading line by line, but I don't really know if that's faster. All I know is it takes less of a toll on my IDE, as having a list of 30000 lines makes any coding sluggish.

5
  • 1
    where are the 3000000 lines coming from? how do they get into python? Commented May 14, 2020 at 5:24
  • see if this helps--> link Commented May 14, 2020 at 5:27
  • you might want to look at the solution implemented here stackoverflow.com/a/39923218/12337794 Commented May 14, 2020 at 5:29
  • 1
    Would it be more efficient to declare/convert your structure as/to a dictionary, so you can do large_dict['maj7'] to do the lookup. BTW, your syntax looks wrong - should there be a close square bracket at the end of the two sample lines? Commented May 14, 2020 at 5:29
  • The lines came from other code that generated the correct attributes @Joran Commented May 14, 2020 at 6:00

2 Answers 2

1
import pandas
# convert it to a dataframe
df = pandas.DataFrame(largeList)
#select all the rows where the zeroth element starts with "maj"
mask = df[0].str.startswith("maj")
print(df[mask]) 
Sign up to request clarification or add additional context in comments.

2 Comments

I hadn't heard of DataFrame, but this looks promising. Thanks!
Please put your answer always in context instead of just pasting code. See here for more details.
0

Hello Bud,

I am pretty new to Python, however, in this case, my best guess to find the smallest of them all sublist in this gigantic monster called large_list is by using the code shared below:

large_list=[['maj', 4, 7], ['3', '5'], ['maj9', 7], ['maj3', 100], ['maj4', 1360], ['maj7', 4, 7, 11], ['3', '5', '7']]
def getTheMinGuy(mylist):
    minimum = min(mylist)
    return print(f"The smaller sublist on this list is => {minimum}")
getTheMinGuy(large_list)

I certainly hope this helps finding the best solution for this issue, champion. Cheers!

1 Comment

how does this help him solve the problem? i dont think this code will do what you think it will do... he wants a sublist containing all the elements that start with maj or some variant of maj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.