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']]
lastwords
  • 3
  • 1

3 Answers3

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))
Austin
  • 25,759
  • 4
  • 25
  • 48
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

Michael
  • 546
  • 1
  • 7
  • 19
  • 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! – lastwords Mar 10 '20 at 04:31
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']]
PacketLoss
  • 5,561
  • 1
  • 9
  • 27
  • Thank you! your upper_list function worked, as my goal was to apply another method. The capitalizing was just an example :) – lastwords Mar 10 '20 at 04:29