0

I would appreciate some help. I have a list of lists. The list containing all lists is called all_lists. In the lists of all_lists are strings. I want to apply a method on the text of each string in each list.

list_in_list = all_lists[0] #prints first list
string_in_list = list_in_list[0] #prints string in first list

I want to create a for loop which will allow me to alter all the strings with a method, and put them back in lists, and puts all lists back in the all_lists. This is what I have for now:

new_list = []
for i in range(len(all_lists)): # all lists
    list_in_list = all_lists[i] # separate lists from all_lists
    for j in range(len(list_in_list)): # every list in all_list
        string_in_list = line[j] # separate string from list
        new_string = decontracted(string_in_list) # apply the method on string
        new_list.append(new_string) # put new string back in a list

I am unsure how to put every list back in a list containing all lists. Can anyone help me do this?

Example if the method was to capitalize every string:

from:

[['list one'],['list two'],['list...'],['list n']]

to:

[['LIST ONE'],['LIST TWO'],['LIST...'],['LIST N']]
2
  • Is there a reason your inner lists have only one item? Commented Mar 10, 2020 at 4:15
  • Yes, I am preprocessing text for a classifier Commented Mar 10, 2020 at 4:19

3 Answers 3

1

You can apply function to each element of list using map:

list(map(lambda x: [x[0].upper()], lst))

Code:

lst = [['list one'],['list two'],['list...'],['list n']]

print(list(map(lambda x: [x[0].upper()], lst)))
# [['LIST ONE'], ['LIST TWO'], ['LIST...'], ['LIST N']]

This is not limited; we can apply any custom function to transform elements in list, on your example:

list(map(decontracted, lst))
Sign up to request clarification or add additional context in comments.

Comments

0

You can use for loops or list comprehension to achieve the result in your example.

all_lists = [['list one'],['list two'],['list...'],['list n']]

As a function, we can perform the upper for every element in the list, using a for loop.

def upper_list(data):
    result = []
    for nested in data:
        changes = []
        for element in nested:
            changes.append(element.upper())
        result.append(changes)
    return result

upper_list(all_lists)
#[['LIST ONE'], ['LIST TWO'], ['LIST...'], ['LIST N']]

Furthermore you can use list comprehension to compress the above into a single line of code.

all_lists = [[element.upper() for element in nested] for nested in all_lists]

Both of these would work over nested lists including multiple elements such as;

all_lists = [['list one', 'test'],['list two','two'],['list...'],['list n']]

>>>[[element.upper() for element in nested] for nested in all_lists]
#[['LIST ONE', 'TEST'], ['LIST TWO', 'TWO'], ['LIST...'], ['LIST N']]

1 Comment

Thank you! your upper_list function worked, as my goal was to apply another method. The capitalizing was just an example :)
0

A functional approach using map and filter would be a useful way. See a similar question below: Apply function to each element of a list

1 Comment

Thanks a lot, map didn't work for me though, since I was trying to apply a method I had previously created. The capitalizing was just an example. The method of PacketLoss worked though, so it's solved!

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.