Python: Create a list of tuples

python tuple
python list
python find in list of tuples
convert list to list of tuples python
python array of tuples
input list of tuples python
python tuple vs list
initialize list of tuples python

I want to create a list of tuples and have this table:

Nr  Name        Value   F/R
1   6347_sx     123.98  F
2   hff_475     234.99  F
3   sjdh_65     123.67  R
4   6347_sx     345.12  R

And I want a list like this:

norm_list = [('6347_sx',123.98), ('hff_475',234.99), ('sjdh_65',123.67), ('6347_sx',345.12)] 

I try this but it did not give me the desired output:

norm_file = open("table.txt")

norm_list = []

for norm_line in norm_file.readlines():
    norm_file_elements = norm_line.split()

    x = norm_file_elements[1]
    y = norm_file_elements[2]
    norm_list= [(x,y)]
    print(norm_list)

y values have to be int. Thank you for any help.

You need to append element to the list inside the loop. In your code example, you are always setting the variable to a list of a single element, instead of appending.

Thus, change your code as follows:

norm_file = open("table.txt")

norm_list = []

for norm_line in norm_file.readlines():
    norm_file_elements = norm_line.split()

    x = norm_file_elements[1]
    y = norm_file_elements[2]
    norm_list.append((x,y))
print(norm_list)

Python program to create a list of tuples from given list having , Python program to create a list of tuples from given list having number and its cube in each tuple. Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number. We can use list comprehension to create a list of tuples Take 4 hours to learn the basics of manipulating time series data. Intro to Data Visualization with Python - Online and On Demand

Try zip function

It would be much easy

print(list(zip(norm_file_elements[1], norm_file_elements[2]))

Python Lists of Tuples, In this post we will talk about creating Python Lists of Tuples and how they can be used. Python Lists. Lists in Python are simply an array. Python list of tuples using zip() function. Python zip() function can be used to map the lists altogether to create a list of tuples using the below command: list(zip(list)) The zip() function returns an iterable of tuples based upon the values passed to it. And, further, the list() function would create a list of those tuples as an output from the zip() function.

You can use re.findall:

s = """
Nr  Name        Value   F/R
1   6347_sx     123.98  F
2   hff_475     234.99  F
3   sjdh_65     123.67  R
4   6347_sx     345.12  R
"""
d = re.findall('\w+_\w+|\d+\.\d+', s)
result = [(d[i], d[i+1]) for i in range(0, len(d), 2)]

Output:

[('6347_sx', '123.98'), ('hff_475', '234.99'), ('sjdh_65', '123.67'), ('6347_sx', '345.12')]

You can also use re.split with unpacking:

t = list(filter(None, s.split('\n')))
_, *data = [(a, b) for _, a, b, _ in map(lambda x:re.split('\s+', x), t)]

Output:

[('6347_sx', '123.98'), ('hff_475', '234.99'), ('sjdh_65', '123.67'), ('6347_sx', '345.12')]

python: create list of tuples from lists, Use the builtin function zip() : In Python 3: z = list(zip(x,y)). In Python 2: z = zip(x,y) . I have two lists: x = ['1', '2', '3'] y = ['a', 'b', 'c'] and I need to create a list of tuples from these lists, as follows: z = [('1','a'), ('2','b'), ('3','c')] I

How do you create an empty list of tuples?, In Python, constructors like list(), set(), tuple() etc. take an iterable as input. Hence they iterate through the iterable and somthing like list(tuple())� Python program to create a list of tuples from given list having number and its cube in each tuple Given a list of numbers of list, write a Python program to create a list of tuples having first element as the number and second element as the cube of the number.

Lists and Tuples, Make a list with three or four items, and then print a message that includes at least one item from your list. Let’s discuss ways to convert string and list data to tuple. Method #1 : Using List conversion to tuple + tuple () In this method, we convert the string to list and then append to target list and then convert this result list to tuple using tuple ().

[Tutor] [tutor] creating list of tuples - Python mailing list, i m creating a tuple of 2 elements everytime and i m adding it to a list. i m creating a new tup everytime and adding it to a list. for x in file:� Python Tuples. Python provides another type that is an ordered collection of objects, called a tuple. Pronunciation varies depending on whom you ask. Some pronounce it as though it were spelled “too-ple” (rhyming with “Mott the Hoople”), and others as though it were spelled “tup-ple” (rhyming with “supple”).

Comments
  • norm_list.append((x,y))
  • norm_list = [tuple(line.split()[1:3]) for line in norm_file]
  • I want y values to be int. How can I get that done?
  • I want y values to be int. How do I get that?
  • y = int(norm_file_elements[2])
  • I want y values to be int. How can I get that done?
  • Cast it into int() datatype like int(norm_file_elements[2])