0

I am trying out python, basically newbie. What I wanted to do is to save or store all generated list into one list and access that list later. The list is generated from a loop.

Basically my code is:

def doSomething():
    list = []
    ....some parsing procedures....
    ......
    ...here needed texts are extracted...
    ...so I am looping to extract all texts I need...

    for a in something:            
        list.append(a)

After the first execution, list is now populated... then the program proceeds into the next page which is basically the same structure and then again invoke the doSomething function. I hope this is now clear..

Assuming the first, second and third loop etc. generated this:

1st loop: [1,2,3]
2nd loop: [4,5,6]
3rd loop: [7,8,9]

I wanted to save these lists into one list and access it later so that:

alllist = [1,2,3,4,5,6,7,8,9]

How can I achieve this?

Chi Chi
  • 23
  • 2
  • 1
    If you're asking "how do I add one list to another list?", there's `list1.extend(list2)` (mutating but fast) and there's `list1 + list2` (nonmutating but slower), among other approaches. – Kevin Mar 15 '18 at 16:04
  • the doSomething function is run in a loop, so I am visioning and I have tried it it only replicates itself – Chi Chi Mar 15 '18 at 16:08
  • Hmm, you lost me. Please provide a [mcve] that demonstrates the replication problem. – Kevin Mar 15 '18 at 16:19
  • Possible duplicate of [How to concatenate two lists in Python?](https://stackoverflow.com/questions/1720421/how-to-concatenate-two-lists-in-python) – AmiNadimi Mar 15 '18 at 17:01
  • @Kevin just to make it clear, the program is actually a scraper, and this particular section of code parses same structure of pages so that's why the function is executed in a loop (to cover pagination).. – Chi Chi Mar 16 '18 at 04:06
  • I have edited and elaborated the question above..hope it make sense – Chi Chi Mar 16 '18 at 04:36

7 Answers7

0

Identation is important in python. Your code does not have proper Indentation. Other programming languages use { and } to group statements whereas Python uses whitespace.

for a in something:
    list.append(a)
for b in something2:
    list.append(b)

However, I would recommend using something + something2 directly.

JR ibkr
  • 869
  • 7
  • 24
0

This should help:

lst = [1, 2, 3]
lst.extend([4, 5, 6])
lst.extend([7, 8, 9])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
Austin
  • 25,759
  • 4
  • 25
  • 48
0

You can store your first three lists from the for loop in variables, and then you can do a for loop for each list, and append it to a new list in order to get the output that you are seeking.

first_list = [1,2,3]
second_list =  [4,5,6]
third_list = [7,8,9]

new_list = []

for number in first_list:
      new_list.append(number)

for number in second_list:
      new_list.append(number)

for number in third_list:
      new_list.append(number)

print(new_list)

The output now in new_list is:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27
  • You can essentially add my code to a function, with all three of the new lists stored as global variables outside of the function, or you don't necessarily have to have a function. The choice is yours. – Simeon Ikudabo Mar 15 '18 at 16:12
  • okay the function will loop around 400x, any easy way to initiate a new variable? – Chi Chi Mar 15 '18 at 16:43
0

you may use extend method:

def doSomething(something):
    list = []
    for a in something:
        list.append(a)
        # ....this loop for some definite number...
    return list

allist = []
allist.extend(doSomething(smt1))
allist.extend(doSomething(smt2))
allist.extend(doSomething(smt3))
Gsk
  • 2,929
  • 5
  • 22
  • 29
  • hi I have tried this but it returns NONE, just to make it clear, the program is actually a scraper, and this particular section of code parses same structure of pages so that's why the function is executed in a loop (to cover pagination).. what parameter should I put in the doSomething() function? – Chi Chi Mar 15 '18 at 16:40
0

What you really need (I think) is what other languages might call a 'static'. There are several solutions to this, including writing a class. I often use a closure for this.

In this example, the first function called, initial sets-up four attributes, a localList (don't call a variable list, it masks the list class), start, and two inner functions. References to those functions are returned (without being called) and they each have localList retained in their context.

The use of nonlocal (Python 3 required) is required to indicate the context of start.

The advantage of this is that the actual mechanism is encapsulated, and we don't have globals. If you had variables called localList, start, inner, and inner_get elsewhere in the program they would not collide.

def initial():
    localList = []
    start = 1

    def inner():
        nonlocal start

        for a in range(start, start + 3):
           localList.append(a)
        start += 3

    def inner_get():
        return localList

    return inner, inner_get

# get the function references
doSomething, getIt  = initial()

for i in range(3):
    doSomething()

# get the list
print(getIt())  
# or
alllist = getIt()

Gives:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Yes its a bit complicated, but its a useful technique to get your head around if you can.

cdarke
  • 42,728
  • 8
  • 80
  • 84
0

I had figure it out, I have to create a function that store all the lists during the loops:

def funcThatStoreList(args, result =[]):
    result.append(args)
Chi Chi
  • 23
  • 2
0
you might pass the values to another function like:

    def newFunction(args, results =[]):
        result.append(args)
        return result

then call the function that generates list:

    doSomething()
    newFunction(doSomething())

if we print newFunction(doSomething()), we will see the appended lists from doSomething function