+ 2

Tuples and Lists

How can I take tuples and make list?

23rd Apr 2021, 3:45 PM
Elena Vitsiou
Elena Vitsiou - avatar
10 odpowiedzi
+ 1
def my_func(*my_tuple): return list(my_tuple) print (my_func (input())) Elena Vitsiou
23rd Apr 2021, 4:17 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 3
Can u explain more what u need ? U need to take tuple from user or anything other? And u want to make the tuple list or what u need ?
23rd Apr 2021, 3:51 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 2
XXX thank you!
23rd Apr 2021, 4:08 PM
Elena Vitsiou
Elena Vitsiou - avatar
+ 1
Dolphin I want to take a tuple from a user and make it a list to change it.
23rd Apr 2021, 3:58 PM
Elena Vitsiou
Elena Vitsiou - avatar
+ 1
Elena Vitsiou You can convert tuple to list by passing it to the list constructor ``` tup = (1, 2, 3) list_tup = list(tup) # list_tup is [1, 2, 3] ```
23rd Apr 2021, 4:01 PM
XXX
XXX - avatar
+ 1
Elena Vitsiou XXX Another Solution without using built-in function like list() function def my_func(*my_tuple): my_list = [] for num in my_tuple : my_list.append(num) return my_list print (my_func (input()))
23rd Apr 2021, 4:38 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
Elena Vitsiou List comprehension along with zip() function is used to convert the tuples to list and create a list of tuples. Python iter() function is used to iterate an element of an object at a time. The 'number' would specify the number of elements to be clubbed into a single tuple to form a list. https://www.askpython.com/python/list/python-list-of-tuples#:~:text=List%20comprehension%20along%20with%20zip,tuple%20to%20form%20a%20list.
23rd Apr 2021, 9:28 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 1
Actually a tuple is a list but the elements are fixed....if you want insertion or deletion use a list and declare it as such.
24th Apr 2021, 5:14 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Dolphin The output of your code will always be a list with 1 string. The OP only asked how to convert a tuple to list, just answer that. Why overcomplicate your answer and still give a solution which is of little use?
23rd Apr 2021, 4:24 PM
XXX
XXX - avatar