0

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

astra
  • 7
  • 2

5 Answers5

0

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)
Tsubasa
  • 1,389
  • 11
  • 21
0

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.

0

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

Mert Kurttutan
  • 355
  • 1
  • 3
  • 7
0

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))
rmutalik
  • 1,925
  • 3
  • 16
  • 20
-1

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))
Jeff
  • 29
  • 3
  • Python has a built-in function called 'type' that allows you to check the type of a variable. – rmutalik Dec 14 '22 at 07:41
  • 1
    Thanks, learned something new today! Apparently `isinstance` can handle inheritance while `type` cannot: [link](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance) – Jeff Dec 14 '22 at 13:52