Python list: check if all items are the same or not
up vote
0
down vote
favorite
I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then
but i need to check all combinations of list values and if list have many items then need to many hard code
if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'
List of string
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
UPDATE
EXAMPLE :
CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
IN correct list all items values are the same, then my list is correct
WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
WRONG_LIST in the wrong list is not all items values string are the some
I want do do this check for list values.
Any idea how to check if all items values in list are the same or not ?
python list
add a comment |
up vote
0
down vote
favorite
I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then
but i need to check all combinations of list values and if list have many items then need to many hard code
if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'
List of string
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
UPDATE
EXAMPLE :
CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
IN correct list all items values are the same, then my list is correct
WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
WRONG_LIST in the wrong list is not all items values string are the some
I want do do this check for list values.
Any idea how to check if all items values in list are the same or not ?
python list
2
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
1
Can you explain with an example?
– Austin
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then
but i need to check all combinations of list values and if list have many items then need to many hard code
if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'
List of string
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
UPDATE
EXAMPLE :
CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
IN correct list all items values are the same, then my list is correct
WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
WRONG_LIST in the wrong list is not all items values string are the some
I want do do this check for list values.
Any idea how to check if all items values in list are the same or not ?
python list
I have a python list with strings and i want to check if all list items values are the same or not.
I tried to use with condition if/then
but i need to check all combinations of list values and if list have many items then need to many hard code
if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'
List of string
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
UPDATE
EXAMPLE :
CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
IN correct list all items values are the same, then my list is correct
WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
WRONG_LIST in the wrong list is not all items values string are the some
I want do do this check for list values.
Any idea how to check if all items values in list are the same or not ?
python list
python list
edited yesterday
iGian
2,2832620
2,2832620
asked yesterday
Mar
236110
236110
2
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
1
Can you explain with an example?
– Austin
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday
add a comment |
2
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
1
Can you explain with an example?
– Austin
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday
2
2
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
1
1
Can you explain with an example?
– Austin
yesterday
Can you explain with an example?
– Austin
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
And returns false
for your test case.
add a comment |
up vote
4
down vote
The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.
One way to do it is to convert it to a set and check its length.
len(set(listOfStrings))
Updated to include working code snippet from @iGian:
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
New contributor
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
add a comment |
up vote
1
down vote
IN correct list all items values are the same, then my list is correct
If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:
set((listOfStrings)) == 1
Characteristic for a set is that every element is unique namely to that set.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
And returns false
for your test case.
add a comment |
up vote
1
down vote
accepted
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
And returns false
for your test case.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
And returns false
for your test case.
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
And returns false
for your test case.
answered yesterday
b-fg
73711022
73711022
add a comment |
add a comment |
up vote
4
down vote
The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.
One way to do it is to convert it to a set and check its length.
len(set(listOfStrings))
Updated to include working code snippet from @iGian:
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
New contributor
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
add a comment |
up vote
4
down vote
The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.
One way to do it is to convert it to a set and check its length.
len(set(listOfStrings))
Updated to include working code snippet from @iGian:
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
New contributor
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
add a comment |
up vote
4
down vote
up vote
4
down vote
The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.
One way to do it is to convert it to a set and check its length.
len(set(listOfStrings))
Updated to include working code snippet from @iGian:
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
New contributor
The code snippet you have provided looks a little weird. But if I understand correctly, you are trying to check for the number of unique values in a list.
One way to do it is to convert it to a set and check its length.
len(set(listOfStrings))
Updated to include working code snippet from @iGian:
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
New contributor
edited yesterday
New contributor
answered yesterday
boonwj
1014
1014
New contributor
New contributor
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
add a comment |
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
1
1
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
check = 'wrong' if len(set(list_of_strings)) > 1 else 'correct'
– iGian
yesterday
add a comment |
up vote
1
down vote
IN correct list all items values are the same, then my list is correct
If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:
set((listOfStrings)) == 1
Characteristic for a set is that every element is unique namely to that set.
add a comment |
up vote
1
down vote
IN correct list all items values are the same, then my list is correct
If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:
set((listOfStrings)) == 1
Characteristic for a set is that every element is unique namely to that set.
add a comment |
up vote
1
down vote
up vote
1
down vote
IN correct list all items values are the same, then my list is correct
If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:
set((listOfStrings)) == 1
Characteristic for a set is that every element is unique namely to that set.
IN correct list all items values are the same, then my list is correct
If you want to check if all items in a list are the same you can check if the lenght of the set of the list is equal to 1:
set((listOfStrings)) == 1
Characteristic for a set is that every element is unique namely to that set.
edited yesterday
answered yesterday
petruz
461311
461311
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238409%2fpython-list-check-if-all-items-are-the-same-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
"i want to check if all list items values is the some or not.", what do you mean with this?
– Willem Van Onsem
yesterday
1
Can you explain with an example?
– Austin
yesterday
Austin and Willem Van Onsem i update my question
– Mar
yesterday