Insert element last into list - list

How do you insert an element (in this case, a char) into a list so it ends up as the last element?
For example, say that I want to add #"D" as the last element in the list [#"A, #"B", #"C"] so I then would have [#"A, #"B", #"C", #"D"].
(This should also work for inserting a string containing more than one element: adding [#"D", #"E"] should give [#"A, #"B", #"C", #"D", #"E"].)

Just append it to the end using "#".
[#"A", #"B", #"C"] # [#"D"]

Related

How can I fix a for loop for a list of words?

I created a pop up using Tkinter that would show all of the words in a list. I used a for loop so that I could insert all of the words in the Listbox that I have set up. The problem that has occurred is that the for loop is putting the words from the list reversed and inserted one letter per index. I wanted that there should be one word in a non reversed order per index.
Code:
for words in list:
index = 0
if words != 0:
index += 1
listbox.insert(index, words)
Output(Check the inside of the list box):
First of all, you should avoid using keyword list as variable. Change it to, for example, wordlist instead. Also you should insert word at the end of listbox instead of index:
for word in wordlist: # better rename list to wordlist
listbox.insert('end', word) # insert at the end of listbox instead

How do I remove duplicate words from a list in python

I have the following list:
Liste=['hello','hello word','word','red','red apple','apple','king']
and I want to remove the duplicate words that include in other words like 'hello' and 'word','red', and 'apple'
so the ResultsList will be like this:['hello word', 'red apple','king']
i tried a few methods but didn't work for me!
So anyone can help with a simple solution to my problem?
myList = ['hello','hello word','word','red','red apple','apple','king']
newList = []
for item in myList:
unique = True
current = myList.pop()
for string in myList:
if current in string:
unique = False
if unique:
newList.append(current)
myList.insert(0, current)
print(newList)
Loop through your list, each iteration pops the last element from the list. Thereafter loop through the remaining elements and evaluate if the string we popped is a substring of any of the other remaining strings.
If not, we consider the string we popped unique and we can append it to an empty list. At the end of each loop iteration insert the string we popped to the beginning of the original list.
set() would work if you were looking to remove exact duplicates and not substrings.

Python - iterate over two lists and find matches and position of mis-matches

I am working in Python 2.7
I am trying to iterate over 2 lists, of un-equal length, and I want to create a new list, containing the matching elements (same elements in the same position), and when the elements do not match, I need to have some text as well as the position of the miss-matching elements.
list1=[1,2,3,4]
list2=[1,2,3,5,6]
This outputs the matches
match=[[b] for a, b in zip(list1, list2) if a==b]
result:
[1,2,3]
But I do not know, in a one-liner, how to also flag the mis-matches:
[1,2,3,"nomatch-pos4"]
or
[1,2,3,"nomatch-pos4","nomatch-pos5"]
It does not matter if it will iterate over the maximum or minimum of the 2 list lengths.
it first find the minimum of the two lists and iterate over the shorter list and check if an element in the list matches with other list in same position. check below code:
match = [list1[i] if list1[i] == list2[i] else 'nomatch-pos'+str(i+1) for i in range(0,min(len(list1),len(list2)))]

Term for sublist of all elements except last?

The sublist, containing all elements, except first is called "tail".
What is the name of sublist of all elements, except first?
Without context, tail is the end of a list. Head is the beginning of a list. Your clarification of "all except the first" must depend on the environment in which you are working. Tail can refer to any number of elements at the end of a list.

How to get list elements by matching string using scala?

I have following list-
List((name1,233,33),(name2,333,22),(name3,444,55),())
I have another string which I want to match with list and get matched elements from list.
There will be only one element in list that matches to given string.
The list may contains some empty elements as given as last element in above list.
Suppose I am maching string 'name2' which will occurs only once in the list, then
My expected output is -
List(name2,333,22)
How do I find matching list element using scala??
.find(_._1 == name2)
will be better
Consider collect over the tuples list, for instance like this,
val a = List(("name1",233,33),("name2",333,22),("name3",444,55),())
Then
a collect {
case v # ("name2",_,_) => v
}
If you want only the first occurrence, use collectFirst. This partial function ignores tuples that do not include 3 items.