0

I'm supposed to add a variable and a boolean to a new Tuple - the actual assignment is below, with my code. I know tuples are immutable - this is the first time I've tried to make one. Additionally, I can't find anything about inserting the variable and a boolean. Thanks in advance!

My code is just created a new list. This is the desired result:

 [('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

Assignment:

The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.

import string
nums = string.digits
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
is_num = []

for item in chars:
    if item in string.digits:
        is_num.insert(item, bool)

elif item not in string.digits:
    is_num.insert(item, bool)
1
  • Use is_num.append. list.insert(i, x). > Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). (docs.python.org/3/tutorial/datastructures.html) Commented May 6, 2020 at 14:33

2 Answers 2

1

You can use a list comprehension for this, which is like a more concise for loop that creates a new list

>>> from string import digits
>>> chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
>>> is_num = [(i, i in digits) for i in chars]
>>> is_num
[('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

This would be equivalent to the follow loop

is_num = []
for i in chars:
    is_num.append((i, i in digits))

>>> is_num
[('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

Note that the containment check is being done using in against string.digits

>>> digits
'0123456789'
>>> '7' in digits
True
>>> 'b' in digits
False
Sign up to request clarification or add additional context in comments.

Comments

0

the easiest way is that you should have casted the nums in to a list. i mean num =list(num) before parsing it in

import string
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
    nums = string.digits
    nums = list(nums)
    is_num = []

for char in chars:
    if char in nums:
        is_num.append((char, True))
    else:
        is_num.append((char, False))
print(is_num)

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.