Remove duplicate values from list in tuple [duplicate] - python

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 4 years ago.
I have a list that containts a tuple, and that tuple contains another list with values:
list
[('5c3b542e2fb59f2ab86aa81d',
['hello', 'this', 'is', 'a', 'test', 'sample', 'this', 'is', 'duplicate'])]
As you can notice 'this and 'is' are already present in this list and I would like to remove them.
So I want something like this:
new_list
[('5c3b542e2fb59f2ab86aa81d',
['hello', 'this', 'is', 'a', 'test', 'sample', 'duplicate'])]
Just to be clear, the List above contains multiple values, see my screenshot attached to this question.
So far I tried the following method:
final_list = []
for post in new_items:
if post[1] not in final_list:
_id = post[0]
text = post[1]
together = _id, text
final_list.append(together)
I tried to loop through each item in the list and if it's not in the list, it would be added to the final_list. But so far this method doesn't work and gives me the exact same list back.

One easy way to remove duplicates from a list is to first convert it to a set (sets cannot hold duplicate elements) and the convert it back to a list. Example
alist = ['a', 'b', 'a']
alist = list(set(alist))
The result of alist will be ['a', 'b']
You can add this in your for loop

Related

Count the number of occurrences different final word types succeeding the string

Suppose I have a list of lists like this
l = [['a', 'paragraph', 'is'],
['a', 'paragraph', 'can'],
['a', 'dog', 'barks']]
also suppose I have this smaller list a = ['a', 'paragraph'] I want to count the number of occurrences different final word types succeeding the string. Therefore, the answer in this case should be 2 since 'is' and 'can' succeed the string 'a paragraph'.
I was trying to do something like this
l.count(a)
but that did not work and gives me 0.
Ill try to spell this idea out more clearly, we basically have this substring 'a paragraph' and there are two occurrences that have 'a paragraph' namely 'is' and 'can', therefore since there is 2 unique cases the answer is 2.
Make a set of all the desired words:
myset = {item[2] for item in l if item[:2] == ['a', 'paragraph']}
Then use len() of the set.
You want to match each item in the list against the smaller list. We start by subsetting the larger list to match the size of the smaller list. If it doesn't match, we continue, ignoring that item. If it does match, we add the next item to a set. The set is important because it handles uniqueness.
items = [
['a', 'paragraph', 'is'],
['a', 'paragraph', 'can'],
['a', 'dog', 'barks']
]
check = ['a', 'paragraph']
check_len = len(check)
unique_words = set()
for item in items:
if item[:check_len] != check:
continue
unique_words.add(item[-1])
print(len(unique_words))
2

Sort elements in list based on length first and alphabetical order second [duplicate]

This question already has answers here:
How to sort a list by length of string followed by alphabetical order?
(6 answers)
Closed 8 months ago.
I have a list with strings. Now, I want to sort the elements in this list based on string length.
proxy_list = ['my', 'list', 'looks', 'like', 'this']
My desired output looks as follows:
desired_output = ['my','like','list','this','looks']
The code below sorts the list based on length, but not on alphabetical order:
print(sorted(proxy_list, key=len))
>>> ['my', 'list', 'like', 'this', 'looks']
How can I sort a list with string based on two things?
use key argument of sorted function. convert each element to a tuple of priority. here (len(s), s), this means len(s) have more priority to s.
proxy_list = ['my', 'list', 'looks', 'like', 'this']
print(sorted(proxy_list, key=lambda s: (len(s), s)))
#>>> ['my', 'like', 'list', 'this', 'looks']

Flatten a lists of list with some lists have multiple values - python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Apply function to each element of a list
(4 answers)
Closed 4 years ago.
I was going through this post, which has some great answers, but it does not work for my situation.
I have a list like this:
my_list = [['Hi'],['Hello'],['How', 'are'], ['you']]
I did the flattening and I am getting this,
my_flat_list = [i[0] for i in my_list]
my_flat_list
>>['Hi', 'Hello', 'How', 'you']
If I don't use i[0] I get in list format again.
The output I need is:
['Hi', 'Hello', 'How are', 'you']
I also tired this post using itertools still not getting my desired results.
How can I get my output?
You need to join the inner lists:
list(map(" ".join, my_list))
#['Hi', 'Hello', 'How are', 'you']
str.join will help you turn every sublist to a single string:
In [1]: [' '.join(sublist) for sublist in my_list]
Out[1]: ['Hi', 'Hello', 'How are', 'you']

How to iteratively append key-value pairs to a dictionary in Python? [duplicate]

This question already has answers here:
Multiple keys per value
(7 answers)
flip keys and values in dictionary python
(3 answers)
Closed 7 years ago.
I have the following code:
list1 = ['a', 'b', 'c']
dict1 = {}
for item in list1:
dict1.update({"letter": item})
print(dict1)
I want to iterate over the list and add each element into the dictionary with the same key for each element. So in this case, the required output should be:
{'letter': 'a', 'letter': 'b', 'letter': 'c'}
But the output I get is:
{'letter': 'c'}
Only the last element is added to the dict. How can I get the required output?
Thanks in advance.
sa=[{"letter":a} for in list1]
sa
[{"letter":a},{"letter":b},{"letter":c}]
But there is no need to do this though
The right method would be to add a list to the key letter
dict1={"letter":[]}
dict1["letter"].extend([a for a in list1])
dict
{"letter":["a","b","c"]}

Splitting each string in a list at spaces in Python [duplicate]

This question already has answers here:
How can I replace a text delimited string list item with multiple list items in a python list?
(12 answers)
Closed 5 years ago.
I've got a list that contains a url and some text in each item of a large list in Python. I'd like to split each item in several items every time a space appears (2-3 spaces per item). There isn't much code to post, its just a list stored in a named variable at the moment. I've tried using the split function but I just can't seem to get it right. Any help would be greatly appreciated!
It's hard to know what you're asking for but I'll give it a shot.
>>> a = ['this is', 'a', 'list with spaces']
>>> [words for segments in a for words in segments.split()]
['this', 'is', 'a', 'list', 'with', 'spaces']
You can try something like that:
>>> items = ['foo bar', 'baz', 'bak foo bar']
>>> new_items = []
>>> for item in items:
... new_items.extend(item.split())
...
>>> new_items
['foo', 'bar', 'baz', 'bak', 'foo', 'bar']

Categories

Resources