2

I tried to remove the third and the fourth list from the list of list in python.

My list of list is below:

List =  [
            ['101', 'Dashboard', '1', '1'],
            ['102', 'Potential Cstomer', '1', '1'],
            ['102-01', 'Potential Cstomer', '1', '1'],
            ['102-02', 'Potential Cstomer Activity', '1', '1']
        ]

After remove the third and fourth element of list, I would like to be like this:

NewList =  [
            ['101', 'Dashboard'],
            ['102', 'Potential Cstomer'],
            ['102-01', 'Potential Cstomer'],
            ['102-02', 'Potential Customer Activity']
        ]

I tried my code like below but it did not make any change.

    NewList     = [list(element) for element in List if element[0] or element[1]]

    print NewList

How should I change my current code to achieve my expected result? Thanks.

4 Answers 4

6

Slice each nested list in a list comprehension. The slice notation starts at index 0 and stops at 1 i.e. [0, 2):

NewList = [element[:2] for element in List]

When the start index is not specified, it is taken as None which is the same as start index of the list when None appears before the first :.

Same as:

NewList = [element[slice(None, 2)] for element in List] # More verbose

In Python 3, you could use extended unpacking to achieve the same thing applying the 'splat' operator *:

NewList = [elements for *elements, _, _ in List]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very for your helpful and detail answer. It works perfect. I've learned something from that. Cheer :)
1

How about this:

 for s in List:
    del s[3]
    del s[2]

That deletes in place.

1 Comment

del s[2:] would clear all but the first two entries more efficiently. You could use del s[2:4] if you only want to remove indices 2 and 3. Note that this solution doesn't copy in any way, so the data is dropped (Moses's solution shallow copies, so the top two levels of the new list are independent).
1

This solution uses negative indexing to allow for arbitrary length sublists, along as the original condition of two trailing digits is maintained.

List =  [
        ['101', 'Dashboard', '1', '1'],
        ['102', 'Potential Cstomer', '1', '1'],
        ['102-01', 'Potential Cstomer', '1', '1'],
        ['102-02', 'Potential Cstomer Activity', '1', '1']
    ]
new_final_list = [i[:-2] for i in List]
for i in new_final_list:
   print(i)

Output:

['101', 'Dashboard'], 
['102', 'Potential Cstomer']
['102-01', 'Potential Cstomer']
['102-02', 'Potential Cstomer Activity']

2 Comments

Minor note on behavior: This removes the last two elements, rather than preserving the first two. When there are a uniform four elements in each sublist, the two operations are equivalent, but if the elements are uneven length, or you performed the operation twice, the behavior is very different. Preserving the first two is safer and idempotent.
@ShadowRanger thank you for pointing that out. While other solutions are more than satisfactory performance-wise, I thought I would include this solution to allow for arbitrary length sublists.
-1

Please reference code below:

Names = [["Tom", 32, 12], ["John", 54, 16], ["James", 52, 15]]
Names_new = []

for i in Names:
    # print(i + )
    del i[0]
    Names_new.append(i)

print(Names_new)

Comments

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.