top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: How do I convert between tuples and lists?

+1 vote
510 views
Python: How do I convert between tuples and lists?
posted Dec 4, 2014 by Amit Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The tuple function converts any sequence (actually, any iterable) into a tuple with the same items in the same order.

For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple(‘abc’) yields (‘a’, ‘b’, ‘c’). If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call tuple when you aren’t sure that an object is already a tuple.

The list function converts any sequence or iterable into a list with the same items in the same order. For example, list((1, 2, 3)) yields [1, 2, 3] and list(‘abc’) yields [‘a’, ‘b’, ‘c’]. If the argument is a list, list makes a copy just like seq[:] would.

answer Dec 29, 2014 by Kali Mishra
Similar Questions
+4 votes

I have a list of tuples where the number of rows in the list and the number of columns in tuples of the list will not be constant. i.e.

list = [(a1,b1, …z1), (a2,b2, …, z2),…. ,(am,bm, … , zm )]. It can be compared to the SQL results, as the number of columns change in the sql, the number of columns change in the list of tuples as well.

I have to iterate through each element of each tuple in the list, perform some checks on the value, convert it and return the modified values as a new list of tuples.

i.e.  list_value = [(‘name1’, 1234, ‘address1’ ), (‘name2’, 5678, ‘address2’), (‘name3’, 1011, ‘addre”ss3’)] 

I need to access each value to check if the value contains a double quote and enclose the string containing double quote with double quotes. The transformation should return

list_value = [(‘name1’, 1234, ‘address1’ ), (‘name2’, 5678, ‘address2’), (‘name3’, 1011, ‘”addre”ss3”’)] 

The simplest approach for me would be to do this:

mod_val = [transform(row) for row in list_value] 
def transform(row): 
   mod_list=[] 
   while index < len(row): 
...    if isinstance(row[index],basestring): 
...       if " in row[index]: 
...          mod_list.append("+row[index]+") 
...    else: 
...       mod_list.append(row[index]) 
...    index = index+1 
... return mod_list 

Is there a way to make the code concise using list comprehension?

+2 votes

How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data

[ ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]

How can I make it be

[ ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
 ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
 ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
 ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything. This was popular on SO but yeah it flattens everything I want to be more selective

def flatten(lst):
 for elem in lst:
 if type(elem) in (tuple, list):
 for i in flatten(elem):
 yield i
 else:
 yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
 item[1] - somehow flatten.

Any Idea or pointer?

...