简体   繁体   中英

Adding tuples to a list without them gaining quotation marks?

I have some variables that look something like this:

Word1 = list[0]
Word2 = list[2]

Each variable will output this when printed:

print(Word1)
#prints ('Example 1', 5, 10, 15, 20)
print(Word2)
#prints ('Example 2', 10, 20, 13, 17)

but as soon as I add these to a list, they gain quotation marks around them

NewList.append(Word1)
NewList.append(Word2)
#Newlist becomes ["('Example 1', 5, 10, 15, 20)", "('Example 2', 10, 20, 13, 17)"] when printed.

Is there any way I can remove the quotation marks that appear when adding them to a list, so that NewList can look like this:

[('Example 1', 5, 10, 15, 20), ('Example 2', 10, 20, 13, 17)]

You can use eval

Word1 = eval(list[0])
Word2 = eval(list[2])

NewList.append(Word1)
NewList.append(Word2)

Output:

[('Example 1', 5, 10, 15, 20), ('Example 2', 10, 20, 13, 17)]

Hope this answers your question!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM