david_10001
david_10001

Reputation: 492

How to remove every second string of each tuple, that is inside a tuple, inside a list?

I have the following code (is working code):

import csv

original_list = [('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05'), ('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08'), ('9K44', '09'), ('MessageYAL', '10'),
 ('DescriptionAUS', '11'), ('S7_2', '12')]

code_list = ['1321', '0A3B','9K44']

grouped_tuples = []
for entry in original_list:
    if entry[0] in code_list:
        new_tuple = []
        new_tuple.append(entry)
        for i in range(original_list.index(entry)+1, len(original_list)):
            if(original_list[i][0] not in code_list):
                new_tuple.append(original_list[i])
            else:
                break
        grouped_tuples.append(tuple(new_tuple))

If I then add on:

for entry in grouped_tuples:
    for item in entry:
        print (item[1])

I get the following list:

01
02
03
04
05
06
07
08
09
10
11
12

What I want to do is remove these numbers from the tuples. So instead of using the above code, I used:

for entry in grouped_tuples:
    for item in entry:
        a = grouped_tuples.remove(item[1])
print (a)

However I get the message: ValueError: list.remove(x): x not in list I know that item[0] is in the list as I just printed it. What is causing this error?

Upvotes: 0

Views: 80

Answers (3)

surajs02
surajs02

Reputation: 471

To get the first element of tuples within a list, one solution could be:

For each tuple in the list:

  1. Convert the tuple to a list a
  2. Store first element from a as a single element in list b tuple

E.g.:

>>> a=[("x","z"),("y","z")]
>>> b=[(list(x)[0],) for x in a]
>>> b
[('x',), ('y',)]

Using this concept with your code gives:

>>> grouped_tuples
[(('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05')), (('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08')), (('9K44', '09'), ('Messag
eYAL', '10'), ('DescriptionAUS', '11'), ('S7_2', '12'))]
>>> #preserve grouped_tuples
... tmpGroupedTuples=list(grouped_tuples)
>>> tmpGroupedTuples_len=len(tmpGroupedTuples)
>>> for i in range(0,tmpGroupedTuples_len):
...     cOuterTuple=list(tmpGroupedTuples[i])
...     cOuterTupleLen=len(cOuterTuple)
...     newOuterTuple=[]
...     for j in range(0,cOuterTupleLen):
...             cInnerTuple=list(cOuterTuple[j])
...             newInnerTuple=((cInnerTuple[0],))
...             newOuterTuple.append(newInnerTuple)
...     tmpGroupedTuples[i]=tuple(newOuterTuple)
...

tmp_grouped_tuples now contains outer tuples containing inner tuples that contain the first element of the original inner tuples of grouped_tuples:

>>> print(tmpGroupedTuples)
[(('1321',), ('MessageXZY',), ('DescriptionSKS',), ('S7_6',), ('S7_3',)), (('0A3B',), ('MessageZYA',), ('DescriptionKAM',)), (('9K44',), ('MessageYAL',), ('DescriptionAUS',), ('S7_2',))]

Upvotes: 0

Nicholas Hamilton
Nicholas Hamilton

Reputation: 10516

Your attempt seems pretty convoluted to me. Unless I have misunderstood what you are trying to achieve, a one liner should be sufficient.

[(k,) for (k,v) in original_list if k in code_list]

Upvotes: 0

ZdaR
ZdaR

Reputation: 22954

You do not necessarily need to remove the elements, you can create a new tuple on the fly with the desired values as:

>>> new_list = [(i[0],)for i in original_list]
>>> [('1321',), ('MessageXZY',), ('DescriptionSKS',), ('S7_6',), ('S7_3',), ('0A3B',), ('MessageZYA',), ('DescriptionKAM',), ('9K44',), ('MessageYAL',), ('DescriptionAUS',), ('S7_2',)]

Upvotes: 3

Related Questions