how do i separate the int and str and store them in their respective tuple

Question:

how do I separate and store the items of a tuple within a tuple to their respective (new) tuple?

the code below is what I’m working.

**the output I want is:

unique_num = (45, 21, 10, 24, 2)
unique_ words = (‘foot’, ‘basket’, ‘hand’, ‘foot’, ‘hand’)**

def unique_data_items(data):
    unique_num = ()
    unique_words = ()
    for items in data:
        for values in items:
            if values == int:
                unique_num= unique_num + values
            elif values == str :
                unique_words= unique_words + values
            
        return ((unique_words), (unique_num))
        
            
data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
print(unique_data_items(data))    

I’ve tried to approach this in a different way and it worked but I wanna know why this way isn’t working

Asked By: astra

||

Answers:

The condition part in your innermost for loop values == int or values == str is not doing what you are thinking. To really check their type use type(values) == int.

Moreover tuples are immutable and you cannot simply concatenate tuple with integer or string values.

Here’s another way of solving this problem.

data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
unique_num = tuple(i[0] for i in data)
unique_words = tuple(i[1] for i in data)

print(unique_num)
print(unique_words)
Answered By: Tsubasa

Outside of including a list within the tuple itself, tuples are considered immutable in Python. There is no .append() method for tuples or any other simple way to change their contents. I could rewrite your program to function as described if you would like, but seeing as you were able to get it to work a different way on your own, I am assuming that that would be unnecessary.

Answered By: Dylan.T.Dawson

If you want a for-loop based version, here it is


def unique_data_items(data):
    unique_words = []
    unique_numbers = []
    for number, word in data:
        unique_words.append(word)
        unique_numbers.append(number)

    return tuple(unique_words), tuple(unique_numbers)

There is also 1 line version, here using zip function

Answered By: Mert Kurttutan

A couple issues here. In Python, tuples are immutable meaning their size can’t change after they’re set. Here you can use a list object.

Also, to check the type of a variable, you need to use something like the isinstance function:

def unique_data_items(data):
    unique_nums = []
    unique_words = []
    for items in data:
        for values in items:
            if isinstance(values, int):
                unique_nums.append(values)
            elif isinstance(values, str):
                unique_words.append(values)
            
    return unique_words, unique_nums
        
            
data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
print(unique_data_items(data))
Answered By: Jeff

Tuples are supposed to be immutable.

You can start off with a list and convert to a tuple at the end.

You need to compare the type(values) to int or str, not the actual value of ‘values’.

Lastly, your return statement was not indented properly

It was cutting off too early after the 1st tuple of items; I moved it back to the previous for loop.

    def unique_data_items(data):
        unique_num = []
        unique_words = []
        for items in data:
            for values in items:
                if type(values) == int:
                    unique_num.append(values)
                elif type(values) == str:
                    unique_words.append(values)
                
        return tuple(unique_words), tuple(unique_num)
            
                
    data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
    print(unique_data_items(data))
Answered By: rmutalik
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.