Nested tuples within lists - python

I have a list of lists of tuples and am having trouble accessing the values within the tuples.
Note: apologies for the basic nature of this query, but I'm a beginner and it takes me a long time to access simple variables.
E.g:
[[('foo', '-1')],[('bar', '-5'),('baz', '+7')], [('qux', '+9')]]
I want to 1) remove the second value of each tuple i.e. the numbers and 2) remove the tuple type and just convert it a list of strings.
As you can see, it is a list of tuples so it can be one tuple or a number of tuples based on the content of extraction - but it is always 2 values within each tuple and I want to remove the second value.
I am having trouble 1) converting each tuple to a string, to be able to 2) remove the second value (preferably I want to remove that before making it into a string) - any ideas?

Just get 0 index of each tuple by iterating in list
ll_tuple = [[('foo', '-1')],[('bar', '-5'),('baz', '+7')], [('qux', '+9')]]
print(
[
[tuple_[0] for tuple_ in l_tuple]
for l_tuple in ll_tuple
]
)

I don't understand very well the question but two points:
Your values in tuple are already string.
To access tuple, you can iterate over your list and access first value of tuple by doing this:
tuple[0]
I don't understand if you have a list of list of tuple, or a list of tuple

First of all your list of lists of tubles is incorrectly formatted I assume what you mean:
data = [
[('foo', '-1')],
[('bar', '-5')],
[('baz', '+7'), ('qux', '+9')]
]
When create a nested for loop and append the first element in each tuble to new list output.
output = []
for lst in data:
for tub in lst:
output.append(tub[0])
print(output)
Prints:
['foo', 'bar', 'baz', 'qux']

Related

Is there a way to insert values into a list of tuple in Python?

I have a empty list of tuple and I wish to enter values inside that tuple.
The desired output is :
lst = [()] --> lst = [(1,2,'string1','string2',3)]
A tuple is, by definition, unchangable.
You may want to replace that tuple with a new one like this:
lst = [()]
lst[0] = ("item1", "item2")
In this way you are replacing the origina tuple with a new one with the desired items. If the tuple is not empty you can do:
lst[0] = (*lst[0], "new item")
Here you are unpacking the values of the old tuple (*lst[0] is equal to "item1", "item2" in this example) and adding new items.
Note that if you are working with variable data maybe tuple is not the best data structure to use in this case.
you can't, A tuple is a collection that is ordered and immutable.
though, you can create a new tuple with the same name

How to enact split on N element of zipped list

I have a zipped list, with each tuple originally having the same length that looks like this:
[('MATH 441', 'GREAT, LOTS OF HOMEWORK, FUNNY'),(CSCI 519, 'AWFUL, BORING')...]
My goal is to split the second element of the tuple into individual elements based on the comma. From here, each tuple may have different lengths, therefore, I would like to check if the given tuple meets a specific length, and if it does not, enter an 'NAN' element into the tuple.
I imagine we must create a "lists of list", for tuples are immutable so this is what I have created thus far.
master = list(zip(course_codez, lst))
#to make it a list of list, not a list of tuples
master_two = [list(a) for a in zip(course_codez, lst)]
for i in master_two:
i[1].split(", ")
This is the thought process, but I am not entirely sure and would appreciate any help.
Thanks
split() doesn't (can't) modify the element in place, you need to assign the result back to the list element.
i[1] = i[1].split(", ")
You can do this directly in the list comprehension.
master_two = [(code, comment.split(", ")) for code, comment in zip(course_codez, lst)]

insert item from an ordered list to a tuple according to index

mylist=[[('The','d'),('apple','n'),('is','v'),('red','a')],[('I','p'),('feel','v'),('fine','adv')]]
This is a list of lists of tuples, and I wish to create a new list of tuples with information from another list added to it accordingly.
new_list=[['b','i','o','o'],['o','o','o']]
for each sub_list of these two lists, I have the exact same number of items as illustrated, and I wish to add the first string of the first list in new_list to the first tuple of the first list in my_list, the second string of the first list in new_list to second tuple of first list in my_list, and so on.
The ideal output looks like this
output=[[('The','d','b'),('apple','n','i'),('is','v','o'),('red','a','o')],[('I','p','o'),('feel','v','o'),('fine','adv','o')]]
I'd appreciate any suggestions, thank you!!
Basically this question has been answered elsewhere, so I flagged the question as duplicate, but assuming that you are new to python, here is a solution to your problem.
output = [[(*t,c) for t,c in zip(l1,l2)] for l1,l2 in zip(mylist,new_list)]
The new output is created using a list nested list comprehension (creating a list of lists). To match corresponding items from two lists together, you can use zip, which returns a generator of tuples of these corresponding items. The for loop that iterates through these tuples unpacks them into two separate items, i.e.
for l1,l2 in zip(mylist,newlist)
groups the sublists of mylist and newlist together, which the for are then unpacked into the lists l1 and l2 during the for iteration. Finally, as the items of the sublists of my_list are tuples, I unpack these tuples 'on the fly' while generating the new tuple: (*t,c) is the same as (t[0],t[1],c).
Please ask if anything stayed unclear.
Hope this helps.

Use a xref of values to sub into tuple based on value - Python

I have a list of xref values
internal_customer = {'01':'11', '03':'33', '05':'55', '07':'77', '08':'88', '06':'66', '09':'22', '11':'18', '12':'19'}
that I would like to use to sub a value in a tuple:
('03', 'S/N A1631703')
So my resulting tuple would be
('33', 'S/N A1631703')
Can someone point me in the direction of the tools I could use to accomplish this?
Unpack and access the dict using the first element, presuming you have an list of tuples:
internal_customer = {'01':'11', '03':'33', '05':'55', '07':'77', '08':'88', '06':'66', '09':'22', '11':'18', '12':'19'}
lst = [('03', 'S/N A1631703'),('05', 'S/N A1631703')]
lst[:] = ((internal_customer[a], b) for a,b in t)
print(t)
tuples are immutable so there is no notion of mutating, you have to create a new tuple comprising of the new value from the dict and the existing second element. The lst[:] syntax at least allows you to modify the original list. You can of course just reassign the name or create a completely new list if you want to maintain the original.

How do you create an empty list of tuples?

I'm trying to create a new empty list that will contain tuples when calling extend. Here's where I declare the list:
ticketData = list()
And I loop through another list to add tuples to this list:
data = (0, requestor, subject, thetime)
ticketData.extend(data)
When I output the result it shows this:
[0, 'Name', 'Test Subject', '03:31:12']
I need it to be a list of tuples, not just a list so that I can use sqlite's executemany function.
It seems straight forward, but I haven't been able to find an answer on here. Thanks!
Just append it to the list, so that the tuple will be added instead of having the elements of the tuple extend the list.
ticketData.append(data)
will add data to the list ticketData
Although this question has been answered already, I thought it could be useful to know about how to construct an iterable that should contain an empty iterable.
In Python, constructors like list(), set(), tuple() etc. take an iterable as input. Hence they iterate through the iterable and somthing like list(tuple()) does not return a list with an empty tuple. Instead the list constructor iterates through the tuples, and since it has no items in it it results in an empty list.
To construct a list that contains an empty tuple do the following:
Instead of my_list = list(tuple()) // returns [] or list()
Do: my_list = [tuple()]
Or: my_list = list([tuple()]) //constructor iterates through the list containing the empty tuple
Analogously, if you want to make a set containing an empty tuple do the following:
Instead of my_set = set(tuple()) // returns set()
Do: my_set = {tuple()}
Or: my_set = set([tuple()]) // constructor iterates through the list, containing the empty tuple
This also applies to other iterables. I am writing this, because I myself have had problems using these constructors to return e.g. a list containing another empty iterable.

Categories

Resources