-2

I have a list of lists each with 11 strings. The last string is an 8-digit code where I need to retain the first three digits but delete the last five.

I'm pretty sure this can be done using indexing and slicing. An example using lists with 5 entries instead of 11.

Example =

[['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]

Desired_Outcome =

[['a','b','c','d','202'],['e','f','g','h','199'],['i','j','k','l', '156']]
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
ajbentley
  • 173
  • 1
  • 8
  • 3
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Oct 02 '16 at 18:10
  • Also, you say you want "to retain the first three digits but delete the last five" and yet your example shows that the first _four_ characters are retained and the last _four_ deleted. – TigerhawkT3 Oct 02 '16 at 18:11
  • I appreciate that but everything I'd written just seemed like such garbage that it wasn't worth sharing. Will add in future, thanks! – ajbentley Oct 02 '16 at 18:14

6 Answers6

2

You can use a nested list comprehension to achieve that

>>> l = [['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]
>>> [j[:-1]+[j[-1][:4]] for j in l]
[['a', 'b', 'c', 'd', '2020'], ['e', 'f', 'g', 'h', '1997'], ['i', 'j', 'k', 'l', '1566']]

The idea here is that you grab only the first 4 characters in the last element.

here j[:-1] first takes the all the elements of the list except the last. j[-1] is the last element and j[-1][:4]] will grab the first 4 characters of the last element.

See What does "list comprehension" mean? How does it work and how can I use it? and Explain Python's slice notation for more details about them.

Community
  • 1
  • 1
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
2

Use list comprehension.

example = [['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]
desired_outcome = [sublist[:-1] + [sublist[-1][:4]] for sublist in example]

And just to your knowledge, the string is an immutable type. It can not be edited. Every time you do something with it, you are creating a new string.

Nf4r
  • 1,330
  • 1
  • 6
  • 9
1

You can edit it place with a for loop and slicing

>>> l = [['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]
>>> for sublist in l:
...     sublist[4] = sublist[4][:4]
... 
>>> l
[['a', 'b', 'c', 'd', '2020'], ['e', 'f', 'g', 'h', '1997'], ['i', 'j', 'k', 'l', '1566']]
>>> 

Note: In your 5 element example, the final element can be indexed as sublist[4] or sublist[-1] (the last element, counting backwards). Both work.... but its best to be consistent throughout the program.

tdelaney
  • 55,698
  • 4
  • 59
  • 89
  • He says there are 11 string in the list, but just 5 in the question but its the last one we need, since we're not sure of the index i think using `sublist[-1]` instead of `sublist[4]` is preferable for the answer. – lycuid Oct 02 '16 at 17:57
  • @Abhishek - yes, that's likely a good way to solve the problem. In a well-defined dataset, the real index and -1 both work equally well so I think I'll keep my example with your comment noted. – tdelaney Oct 02 '16 at 18:01
  • 1
    @BhargavRao I'm remaining true to the example man! – tdelaney Oct 02 '16 at 18:03
  • The question's been asked in a really confusing way, so it doesn't matter, the code works just fine. – lycuid Oct 02 '16 at 18:18
1

Alternatively, you may also achieve the same using map() as:

>>> example = [['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]
>>> map(lambda x: x[:3]+[x[4][:-4]], example)
[['a', 'b', 'c', '2020'], ['e', 'f', 'g', '1997'], ['i', 'j', 'k', '1566']]

Above solution will work on Python 2.7. In case you are using Python 3.x, you need to explicitly add a list() call to map as:

list(map(lambda x: x[:3]+[x[4][:-4]], example))

Check Blog on Lambda, filter, reduce and map to know how lambda functions and map() works in Python.

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Anonymous
  • 40,020
  • 8
  • 82
  • 111
0

You can't edit a string; they are immutable.

But you can slice off the unwanted parts (which creates a new string) from the last item of each sublist:

lst = [['a','b','c','d','2020BG1C'],['e','f','g','h','1997AF33'],['i','j','k','l', '1566IL2L']]
for sublist in lst:
     sublist[-1] = sublist[-1][:4]

print(lst)
# [['a', 'b', 'c', 'd', '2020'], ['e', 'f', 'g', 'h', '1997'], ['i', 'j', 'k', 'l', '1566']]

Some reference:

  1. Explain Python's slice notation
  2. Aren't Python strings immutable?
Community
  • 1
  • 1
Moses Koledoye
  • 71,737
  • 8
  • 101
  • 114
0

Actually its very simple, (1) Loop through outer array. (2) Get the last element of each sub array. (3) slice it using x[:-5]

DHRUV GUPTA
  • 1,574
  • 1
  • 11
  • 22